Graphql-tools: Stitching schema without a Query shows "Query root type must be provided."

Created on 1 May 2018  ·  27Comments  ·  Source: ardatan/graphql-tools

Discription

I am trying to stitch two schemas that one of them only contains a Mutation type using the latestgraphql-tools v3.0.0. The error will disappear if any Query type contains more than one field is added to the single-mutation schema.

Since the error is not the same as #659 and that issue has been a while before the v3.0.0 was out, I open this issue. Please close this if you think this is duplicate.

Related issue: #659
Related PR: #746

Intended outcome

Querying Mutation test field should return a string result.

Actual outcome

"Query root type must be provided." error is returned.

How to reproduce the issue

First schema:

type Query {
  hello: String!
}

Second schema:

type Mutation {
  test: String!
}

Query:

mutation {
  test
}

returned data:

{
  "errors": [
    {
      "message": "Query root type must be provided.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "test"
      ]
    }
  ],
  "data": null
}
schema stitching

Most helpful comment

We solves this by just filling in

type Query {
  dummy: String
}

All 27 comments

This should be fixed with #527 (see comment here: https://github.com/apollographql/graphql-tools/pull/746#issuecomment-383051876). Have you tried bumping to version 3.0.0?

Yes, I am using graphql-tools v3.0.0.

Here is my reproduction repo:
https://github.com/stomvi/graphql-tools-mutation-stitching

Using express along with express-graphql:

git clone [email protected]:stomvi/graphql-tools-mutation-stitching.git
cd graphql-tools-mutation-stitching
yarn install
yarn start

As I described, the schemas to be merged and the merging is located in the file src/graphql/schema.js. The graphiql will be running at locahost on port 8080. And if you execute an mutation query with:
http://localhost:8080/graphql?query=mutation%20%7B%0A%20%20test%0A%7D

you'll get this response:

{
  "errors": [
    {
      "message": "Query root type must be provided.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "test"
      ]
    }
  ],
  "data": null
}

Please kindly take a look. Thanks!!

I am experiencing the same issue. I have numerous schemas that I will stitch together and some of them only have mutations. In my case, makeExecutableSchema throws this error when there is no Query root, which is true because I only have mutations in this particular schema. Currently using 3.0.5

same issue here.

We solves this by just filling in

type Query {
  dummy: String
}

+1

We are also seeing this issue and are basically doing what @simlu suggested.

Also seeing this issue.

I only used the mutation method and experienced this problem when I added a simple query method to test the problem was fixed.

@simlu yes it's work by put type Query in typeDefs and then put it in ApolloServer
const typeDefs = gql type Me { name: String money: Boolean } type Query { anything: String } ;
const server = new ApolloServer({
typeDefs
});
server.listen().then(({ url }) => {
console.log(Listen on port ${url});
});

We experience the same problem, we are using remote schema stitching and one of services we merge with doesn't have a queries, it has only mutations.
We are using GraphQL as our contract which we are sharing with 3rd parties and having 'dummy' or other temp types defined in it looks really bad.
Is there any chance to fix this?

until this is resolved some other way, I added _dummy Query type fields to my mutation only schemas..

then, after stitching:

const schema = transformSchema(mergedSchemas, [
  new TransformRootFields(
    (_, fieldName) => fieldName == "_dummy" ? null : undefined
  ),
]);

and now there is no more exposed _dummy.

There should still be a better way to do this.

Guys any plan in roadmap to fix this?

:D hahahaha it is insane :D

I am trying to build schema based on Types only, so i can use it to build more complex relations out of it and pass it to Neo4J graph database.Instead i need to define Root query which will not be used for anything. Basically i decided to create status instead of Root which returns 200

https://github.com/rxdi/neo4j/blob/master/src/services/util.service.ts#L52

  createRootSchema() {
    const directives =
      this.gqlConfig.directives || this.config.directives || [];
    return new GraphQLSchema({
      query: new GraphQLObjectType({
        name: "status",
        fields: {
          status: {
            type: GraphQLString,
            resolve: () => ({status: 200})
          }
        }
      }),
      directives,
      types: this.typeService.types || []
    });
  }

I would love to transfer it to this

  createRootSchema() {
    const directives =
      this.gqlConfig.directives || this.config.directives || [];
    return new GraphQLSchema({
      directives,
      types: this.typeService.types || []
    });
  }

If i don't define root query this schema is invalid. But i would like to just build schema based on type definition and then just print it out so i can work with the types for further more extending capabilities .

Regards!

so.. any plans to fix this? 😞

so.. any plans to fix this? 😞

+1

bump

bump

Try this one, It might help you out..

const { ApolloServer, gql } = require("apollo-server");

const todos = [{ task: "Open the door", completed: true }];

const typeDefs = gql`
type Todo {
task: String!
completed: Boolean!
}

type Query {
getTodos: [Todo]
}
`;

const server = new ApolloServer({ typeDefs });
server.listen().then(({ url }) => {
console.log(Server is listening ${url});
});

still not fixed

type Query {
  hello: String!
}

works fine

type Mutation {
  test: String!
}

throws error

This cannot be "fixed" without changing overall design. Schema stitching is working as intended in this scenario, the "bug" is that core graphql does not allow schemas without a root query type. See above workarounds by @simlu and @derekpitt. It might look cleaner to expose a __version or __servicename or __status field instead of __dummy in the service, but nothing has to be exposed after stitching if transforms are used per @derekpitt.

You can also try the FilterRootFields transform which just uses TransformRootFields under the hood.

there are a lot of other types besides of Query. Mytations, enums, Input whad makes Query so special?
because i believe even empty file should not throw exception

That is issue for upstream graphql-js reference implementation

https://github.com/graphql/graphql-js/issues/448

I'm still getting this error with v4.0.8 and have to use
type Query {
dummy: String
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

freiksenet picture freiksenet  ·  4Comments

dcworldwide picture dcworldwide  ·  4Comments

Adherentman picture Adherentman  ·  4Comments

benjaminhon picture benjaminhon  ·  3Comments

ghost picture ghost  ·  3Comments