【发布时间】:2019-11-12 16:48:30
【问题描述】:
我在 react 项目中使用 apollo-client 来管理 UI 状态。我为 apollo 突变定义了一个模式类型,但它似乎不起作用。
下面是我如何创建apollo client 实例。
const cache = new InMemoryCache();
export const createClient = () => {
return new ApolloClient({
cache,
resolvers: {
Mutation: {
...alertResolvers
},
},
typeDefs: [alertTypeDefs]
});
};
下面的代码是类型模式定义。如您所见,我创建了一个showErrorAlert 返回类型为Alert。
export const alertTypeDefs = gql`
type Alert {
id: ID!
message: String!
type: String!
duration: Int!
}
extend type Mutation {
showErrorAlert(message: String!): Alert
}
`;
我使用下面的代码发送突变。如您所见,它没有在返回对象中返回duration。但是该应用程序可以正常工作。该类型似乎对应用程序没有影响。
gql`
mutation showErrorAlert($message: String!) {
showErrorAlert(message: $message) @client {
id
message
type
}
}
`;
【问题讨论】:
标签: graphql apollo react-apollo apollo-client