Reputation: 1319
I'm using mongodb and graphql-compose-mongoose in order to generate the Graphql schema. However I'm adding authentication mutations and I want to add graphql input for one of the queries. I saw in the documentation that args.filter
can receive an input:
CityTC.addResolver({
kind: 'query',
name: 'findMany',
args: {
filter: `input CityFilterInput {
code: String!
}`,
limit: {
type: 'Int',
defaultValue: 20,
},
skip: 'Int',
// ... other args if needed
},
type: [CityTC], // array of cities
resolve: async ({ args, context }) => {
return context.someCityDB
.findMany(args.filter)
.limit(args.limit)
.skip(args.skip);
},
});
However I'm not using any filter. I want to create an input like this:
const AuthPayloadTC = schemaComposer.createObjectTC({
name: 'AuthPayloadTC',
fields: {
jwtToken: 'String!'
}
});
AuthPayloadTC.addResolver({
name: 'userConnectData',
type: AuthPayloadTC,
args: `input {
accessToken String!
provider String!
}`,
resolve: async ({ source, args, context, info }) => {
const { accessToken, provider } = args;
return {
jwtToken: '12345678'
}
}
})
This doesn't work because my input is not parsed to a the kind of object that args
field expects. I did notice that graphql-compose exposes InputTypeComposer however I couldn't any example on how to use it in a resolver.
Upvotes: 1
Views: 2623
Reputation: 1319
Through some trial and error I figured out this out:
import { schemaComposer, toInputObjectType } from 'graphql-compose'
const InputTC = schemaComposer.createObjectTC({
name: 'UserConnectDataInput',
fields: {
accessToken: 'String!'
}
});
const InputITC = toInputObjectType(InputTC);
AuthPayloadTC.addResolver({
name: 'userConnectData',
type: AuthPayloadTC,
args: {
input: InputITC
},
resolve: async ({ source, args, context, info }) => {
const { accessToken, provider, email } = args;
return {
jwtToken: '12345678'
}
}
})
Upvotes: 5