Search code examples
interfacegraphqlreact-apollographene-pythongraphene-django

Common fields on graphql interface type in react apollo with a graphene backend


I have a python graphene-django backend with an interface and two types, let's say

interface InterfaceType {
  id: ID!
  name: String!
}

 type Type1 implements InterfaceType {
    aField: String
}

 type Type2 implements InterfaceType {
    anotherField: String
}

I'm able to query this from my react-apollo frontend using inline fragments:

query {
  interfaceQuery {
    ...on Type1 {
      id
      name
    }
    ...on Type1 {
      id
      name
    }
  }
}

But from what I understand it should also be possible to query the common fields simply as

query {
  interfaceQuery 
    id
    name
  }
}

When I try this, however, I get the error Cannot query field "id" on type "InterfaceType". Did you mean to use an inline fragment on "Type1" or "Type2"?

I'm using an IntrospectionFragmentMatcher.

Am I misunderstanding and this kind of simple access of common fields is not possible, or is it just not implemented in either graphene or apollo?


Solution

  • If you're seeing that error, it's coming from the server, not Apollo. However, there's nothing specific to either Apollo or Graphene that should prevent you from querying the interface fields without a fragment.

    The error is thrown because whatever schema you're using literally doesn't have that field on the provided type. Maybe you updated your schema without restarting the service, or were mistaken about what fields were actually part of the interface -- it's hard to know with only pseudocode provided.