graphql / graphql-spec

GraphQL is a query language and execution engine tied to any backend service.
https://spec.graphql.org
14.31k stars 1.13k forks source link

Add 5.2.4 Operation Type Exists #1098

Closed Shane32 closed 1 month ago

Shane32 commented 6 months ago

Closes #1097

netlify[bot] commented 6 months ago

Deploy Preview for graphql-spec-draft ready!

Name Link
Latest commit b9f1f3305e395bb522b85e412d0772476803f857
Latest deploy log https://app.netlify.com/sites/graphql-spec-draft/deploys/67007a357700f600088a42dc
Deploy Preview https://deploy-preview-1098--graphql-spec-draft.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

benjie commented 2 months ago

The lack of this validation rule can be demonstrated in GraphQL.js with this:

import { GraphQLSchema, GraphQLInt, graphql, GraphQLObjectType } from 'graphql';

const Query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    a: {
      type: GraphQLInt,
      resolve() {
        return 42;
      },
    },
  },
});

const schema = new GraphQLSchema({
  query: Query,
});

const result = await graphql({ schema, source: `mutation { __typename }` });
console.dir(result);

which produces:

{
  data: null,
  errors: [
    GraphQLError: Schema is not configured to execute mutation operation.
        at executeOperation [...]
      path: undefined,
      locations: [Array],
      extensions: [Object: null prototype] {}
    }
  ]
}

We should really mark this as an invalid request, and thus it should not have a data key in the response.

yaacovCR commented 1 month ago

This is a refresh of #955 which has a pending PR at the implementation https://github.com/graphql/graphql-js/pull/3601

benjie commented 1 month ago

Good spot @yaacovCR; it seems both @Shane32 and I had commented on that previous PR!

I have merged this PR into that PR and applied my own editorial edits on top of both: