Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stitching schema without a Query shows "Query root type must be provided." #764

Closed
stomvi opened this issue May 1, 2018 · 32 comments
Closed

Comments

@stomvi
Copy link

stomvi commented May 1, 2018

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
}
@stomvi stomvi changed the title Stitching schema without Query shows "Query root type must be provided." Stitching schema without a Query shows "Query root type must be provided." May 2, 2018
@mfix22
Copy link
Contributor

mfix22 commented May 2, 2018

This should be fixed with #527 (see comment here: #746 (comment)). Have you tried bumping to version 3.0.0?

@stomvi
Copy link
Author

stomvi commented May 3, 2018

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 git@github.com: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!!

@mikebm
Copy link

mikebm commented Jul 18, 2018

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

@viztor
Copy link

viztor commented Aug 17, 2018

same issue here.

@simlu
Copy link

simlu commented Sep 7, 2018

We solves this by just filling in

type Query {
  dummy: String
}

@mikepuglisi
Copy link

+1

@austinkelleher
Copy link
Contributor

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

@ezyang
Copy link

ezyang commented Oct 16, 2018

Also seeing this issue.

@kolserdav
Copy link

kolserdav commented Dec 4, 2018

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

@SirawichDev
Copy link

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

@andrew-makarenko
Copy link

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?

@derekpitt
Copy link

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.

@mnlbox
Copy link

mnlbox commented Mar 21, 2019

Guys any plan in roadmap to fix this?

@Stradivario
Copy link

: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!

@GiancarlosIO
Copy link

so.. any plans to fix this? 😞

@secmohammed
Copy link

so.. any plans to fix this? 😞

+1

@maxwellsmart84
Copy link

bump

1 similar comment
@hashk99
Copy link

hashk99 commented Oct 16, 2019

bump

@arshad03490
Copy link

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});
});

@TrejGun
Copy link

TrejGun commented Dec 13, 2019

still not fixed

type Query {
  hello: String!
}

works fine

type Mutation {
  test: String!
}

throws error

@yaacovCR
Copy link
Collaborator

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.

@TrejGun
Copy link

TrejGun commented Dec 13, 2019

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

@yaacovCR
Copy link
Collaborator

yaacovCR commented Dec 13, 2019

That is issue for upstream graphql-js reference implementation

graphql/graphql-js#448

@yaacovCR
Copy link
Collaborator

Closed with this comment: graphql/graphql-js#448 (comment)

@tronxdev
Copy link

@yaacovCR
Copy link
Collaborator

yaacovCR commented Apr 1, 2020

Can be tracked at spec at graphql/graphql-spec#218 and graphql/graphql-spec#490

@UchihaYuki
Copy link

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

@Spolja
Copy link

Spolja commented Jan 9, 2021

There is still the issue if you try to stitch a schema that contains no queries, in my case a specific schema contains only subscriptions. Adding dummy query as previous comments suggested makes it work properly.

Using graphql-tools v7.0.2

@ardatan
Copy link
Owner

ardatan commented Jan 9, 2021

@Spolja schema stitching uses GraphQLJS and GraphQLjs needs a query root type provided, so I don't think we can fix that on our side.

@Spolja
Copy link

Spolja commented Jan 9, 2021

@ardatan,
Yep, when I think about it why it behaves like it does, makes perfect sense 👍

@shubhsk88
Copy link

I am also getting this issue doesn't anybody have working solution??

@aaelius
Copy link

aaelius commented Dec 20, 2021

Until a better solution is known, I resolved it by adding
type Query { _: String }
to the only schema in the array of stitched schemas that did not have a query in it. So now all my subSchemas contain at least a dummy Query type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests