Reputation: 22553
I'm using the graphql npm package to build up a graphql schema that includes a mutation that looks like this:
type Mutation {
cxUnitCosts_onSave(data: [CxUnitCostInput]): [CxUnitCost]
}
which I do like this, assuming I have an input and output variables containing GraphQLObjectTypes:
new GraphQLObjectType({
name: 'Mutation',
fields: {
[camelcase(output.toString())+'s_onSave']: {
args: {data: {type: new GraphQLList(input)},
},
type: new GraphQLList(output)
}
}
})
But what I want is to extend the type mutation to produce something like this:
extend type Mutation {
cxUnitCosts_onSave(data: [CxUnitCostInput]): [CxUnitCost]
}
I need to extend Mutation because I am merging together a large number of schema programmatically generated from JSON Schema (many dozens of cxFoo...) If I don't extend the mutation, only one survives the merge.
I would rather not use String manipulation as in the answer below and I would prefer not to have to do the merging manually. I don't want to have to look inside the schema.
For the life of me I can't figure out how.
Upvotes: 4
Views: 2127
Reputation: 22553
I'm sad I didn't get an answer to my question. What I wound up doing was, in the penultimate step of my pipeline, just before I write the schema to the file, I replace the strings.
replace('type Mutation', 'extend type Mutation'),
replace('type Query', 'extend type Query'),
I guess that's how it goes sometimes. I'm still surprised this isn't doable programmatically.
Upvotes: 0
Reputation: 84687
I believe you can do something along these lines:
const { parse, GraphQLSchema } = require('graphql')
const { extendSchema } = require('graphql/utilities)
const schema = new GraphQLSchema({
query: queryType,
});
const extension = `extend type Foo {
someNewField: String
}`;
const extendedSchema = extendSchema(schema, parse(extension));
But... for a mutation or anything requiring a custom resolver, you would still have to add the resolver back in with something like addResolveFunctionsToSchema
from graphql-tools
.
You can easily extend types if you write your schema using SDL, write your resolvers separately and then create the schema with makeExecutableSchema
instead of defining the schema programatically.
If you provide more context around why you're extending the schema or what you're trying to accomplish, I might be able to suggest a better approach.
Upvotes: 3