The following code-first GraphQL schema example is taken from GraphQL's Constructing Types page.
// Define the Query type
var queryType = new graphql.GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: userType,
// `args` describes the arguments that the `user` query accepts
args: {
id: { type: graphql.GraphQLString }
},
resolve: (_, {id}) => {
return fakeDatabase[id];
}
}
}
});
Although you are able to define the args and their GraphQL types (GraphQLString in this example), the actual id property within the resolver has an "any" type.
I tried replacing {id}
with {id}: {id: string}
but that threw the error Type '(_: any, { id }: { id: string; })' is not assignable to type 'GraphQLFieldResolver<any, any, { [argName: string]: any; }>'
Type '(_: any, { id }: { id: string; })' is not assignable to type 'GraphQLFieldResolver<any, any, { [argName: string]: any; }>'
.
Although this is a simple "string" example, my actual code has more complex object types with multiple properties. Ensuring the correct property name is referenced is a big help elsewhere that is missing within the resolvers. Perhaps there is a way to tell the resolver the actual types of the args but I'm too new to TypeScript to find it.
Is there a way to define/override the types of the resolvers args?
I believe you are having the same problem outlined in this issue .
A workaround is to make your id
property optional (by replacing {id}
with {id}: {id?: string}
) and check for its existence within the resolver before you reference it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.