graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Discriminator type issues

Open arumry opened this issue 6 years ago • 3 comments

As of right now, the base interface returned creates types rather than interfaces for nested objects. Due to this, there is no way to do things like https://graphql.github.io/graphql-spec/June2018/#example-5cc55

Was this the intention?

arumry avatar Apr 08 '19 06:04 arumry

Can you provide a real example where it can be tested?

Nested interfaces should work and if not then it's a bug.

nodkz avatar Apr 08 '19 07:04 nodkz

Hi @nodkz thanks for getting back to me. I added a failing example to https://github.com/bloodhawk/graphql-compose-examples

I then went to http://localhost:3000/mongooseDiscriminators-playground and tried to run:

query {
  characterById(_id: "hi") {
  	...on Droid {
      aNestedField {
        aDroidNestedField
      }
    }
  }
}

and was greeted with the error:

{
  "error": {
    "errors": [
      {
        "message": "Cannot query field \"aDroidNestedField\" on type \"CharacterANestedField\". Did you mean \"anotherGenericNestedField\"?",
        "stack": [
          "Cannot query field \"aDroidNestedField\" on type \"CharacterANestedField\". Did you mean \"anotherGenericNestedField\"?",
          "",
          "GraphQL request (5:9)",
          "4:       aNestedField {",
          "5:         aDroidNestedField",
          "           ^",
          "6:       }",
          "",
          "    at Object.Field (/Users/aaronrumery/Documents/testing/graphql-compose-examples/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:64:31)",
          "    at Object.enter (/Users/aaronrumery/Documents/testing/graphql-compose-examples/node_modules/graphql/language/visitor.js:334:29)",
          "    at Object.enter (/Users/aaronrumery/Documents/testing/graphql-compose-examples/node_modules/graphql/language/visitor.js:385:25)",
          "    at visit (/Users/aaronrumery/Documents/testing/graphql-compose-examples/node_modules/graphql/language/visitor.js:252:26)",
          "    at validate (/Users/aaronrumery/Documents/testing/graphql-compose-examples/node_modules/graphql/validation/validate.js:63:22)",
          "    at /Users/aaronrumery/Documents/testing/graphql-compose-examples/node_modules/express-graphql/dist/index.js:154:52",
          "    at process._tickCallback (internal/process/next_tick.js:68:7)"
        ]
      }
    ]
  }
}

arumry avatar Apr 08 '19 12:04 arumry

Here is a working example:

interface someCommonNestedInterface {
  a: String
}
interface Searchable {
  searchPreviewText: String!
  nestedField: someCommonNestedInterface
}

type BookNestedField implements someCommonNestedInterface {
  a: String
  d: String
}
type Book implements Searchable {
  searchPreviewText: String!
  author: String!
  nestedField: BookNestedField
}

type MovieNestedField implements someCommonNestedInterface {
  a: String
  c: String
}
type Movie implements Searchable {
  searchPreviewText: String!
  directory: String!
  nestedField: MovieNestedField
}

type UserNestedField implements someCommonNestedInterface {
  a: String
  b: String
}
type User implements Searchable {
  searchPreviewText: String!
  username: String!
  nestedField: UserNestedField
}

type Query {
  search(text: String!): [Searchable]!
}

with query

query {
  search(text: "hi") {
    searchPreviewText
    ...on Book {
      nestedField {
        d
      }
    }
    nestedField {
      a
    }
  }
}

or

query {
  search(text: "hi") {
    searchPreviewText
    nestedField {
      ...on UserNestedField {
        b
      }
      ...on BookNestedField {
        d
      }
    }
  }
}

arumry avatar Apr 08 '19 12:04 arumry