Mayank1791989 / gql

112 stars 19 forks source link

Support `extend type` #45

Closed victorandree closed 1 year ago

victorandree commented 7 years ago

This is used by graphql-tools to modularize, primarily, the main Query type:

type Query {
  bars: [Bar]!
}

type Bar {
  id: String!
}

type Foo {
  id: String!
}
extend type Query {
  foos: [Foo]!
}

Would be great if this were supported. The extend keyword seems undocumented but is parsed as part of the AST, producing a TypeExtensionDefinition object, which then holds a regular ObjectTypeDefinition.

graphql includes a utility function graphql/utilities/extendSchema to support extensions. It appears like graphql-tools uses the graphql provided function extendSchema as part of building its complete schema. It should be possible to use similar logic as https://github.com/apollographql/graphql-tools/blob/master/src/schemaGenerator.ts#L147 to utilize these extensions (maybe even use the schema generator from graphql-tools?).

However, this project uses a patched version of buildASTSchema to generate its own GQLSchema instance. extendSchema checks that the passed instance is a GraphQLSchema -- so it fails. It may be the case that you have to use a patched version of extendSchema as well, then...

I'd be willing to look into a patch if there's interest.

Mayank1791989 commented 7 years ago

Looks valid will fix in next release.

maybe even use the schema generator from graphql-tools?

Its not possible as it throws if there is any error which is bad for language service, so need to implement patched version of extendSchema.

lensbart commented 5 years ago

Hi,

Thanks for creating this tool. It looks like the issue has been fixed in the commits of April 2018, but v3 hasn’t been released yet. Do you have an estimate as to when it might get released?

Thanks again!

allesklarbeidir commented 4 years ago

I think this is a big issue because at least I find myself using "extend TYPE" quite often and I can imagine other people doing that as well. At first I thought the graphql-for-vscode was not working because I didn't get any autocompletions and then I found out that the "extend TYPE" breaks it.

I dove into the source and just added some lines of code and now it's working for me. That's kind of a "quick and dirty fix" and I don't know if that introduces other issues but I highly doubt it, because as @victorandree mentioned the AST generated from "extend TYPE" is just the same "ObjectTypeDefinition" as usual but wrapped in a "TypeExtensionDefinition":

{
    "kind": "TypeExtensionDefinition",
    "definition": {
        "kind": "ObjectTypeDefinition",
     .....
     }
}

So adding

case 'TypeExtensionDefinition': {
    const _d = d.definition;
    const name = _d.name.value;
    if (!nodeMap[name]) {
      typeDefs.push(_d);
      nodeMap[name] = _d;
    } else {
      const existingTd = typeDefs.filter((td) => td.name.value === name)[0] || null;
      if (existingTd) {
        existingTd.fields = [].concat(...[...existingTd.fields, (_d.fields || [])]);
      }
    }
    break;
}

in buildASTSchema.js#L162 should fix the issue..

I will leave the hint here and add the fix to a fork / open a PR as soon as I got time to do so.

You can use "yarn add -D allesklarbeidir/gql" if you want to test it right now.

lensbart commented 4 years ago

@Mayank1791989 Apologies for bothering you with this, but would it be possible to incorporate the proposed changes so that @playlyfe/gql can work with schema stitching? This will help kumar-harsh.graphql-for-vscode (300.000+ installs in VSCode) to enable schema stitching as well.

Please let me know if I can be of any assistance.

Thanks in advance!

arackaf commented 3 years ago

For those winding up here, trying to get your VS Code plugin working, it looks like this project is largely abandoned, with a more current alternate plugin here:

https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql

victorandree commented 1 year ago

As the author of the issue, I'm going to close it, since the project appears abandoned.