4

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.

2 Answers 2

3
+100

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I added a bit of context. I'm pretty committed to the path of defining the schema programatically. I'm transforming a bunch of json schema and I really don't want to be emitting strings to do that.
At the end of the day, you just need a single object with properties that map to your Mutation field names. Maybe I'm missing something. Is there a reason you can't reduce your json schema to an object like that, and then finally pass that object in to the constructor for your Mutation type?
I could, I suppose. I have many json schema that I process independently and would rather merge them together using the graphql tools and not have to look inside.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.