Relay: [Relay Modern] Querying __type

Created on 2 May 2017  路  10Comments  路  Source: facebook/relay

With Relay Classic, I used to query some fields from the schema directly, for instance I had:
``js const DiscussionTopicQueries = { platformType: () => Relay.QLquery { __type(name: "Platform") } `,
};

and in my Relay Classic Container I would have:
```js
export default const Relay.createContainer(Component, {
  fragments: {
    platformType: () => Relay.QL`
      fragment on __Type {
        ${ResponseNew.getFragment("platformType")}
      }
    `
    }
});

Platform type being an enum :

enum Platform {
  # iOS
  IOS

  # Android
  ANDROID

  # All of them
  ALL
}

Now with Relay Modern / Relay Compiler, when trying to query on Type, I get:

Error: You supplied a GraphQL document with validation errors:
Fragment "DiscussionDetail_platformType" cannot be spread here as objects of type "Query" can never be of type "__Type".

Is is still possible to query things from the Schema in Relay Modern?

Most helpful comment

I copied from the Schema Introspection these couple of lines of code in the query definition:

__schema: __Schema!
__type(name: String!): __Type

also I copied the whole Schema of the GraphQL schema introspection system from the same section (in the link above).

All 10 comments

figured I could just query __type(name: "Whatever") on any query

Sorry to reopen this years later, but I am getting this error when attempting to query __type:
Unknown field '__type' on type 'Query'.
This is the query I'm trying to compile:

query {
  __type(name: "ISO3166") {
    enumValues {
      name
      description
    }
  }
}

Basically I'm trying to get a list of available enum values for the enum "ISO3166" from the schema. Am I doing something wrong here? This is a valid GraphQL query that returns results from my API.

You can鈥檛 do this anymore, as relay now only rely on types declared on schema.graphql

Expose this in a different way

@sibelius What's the replacement of GraphQL __type introspection?
I can't find anything that helps with the migration of this deprecated feature.

you need to explicit create __type again in your Query Type

Thanks, but I have to mention that the issue was resolved following the definitions found in GraphQL's docs Introspection section.

can you share your solution?

I copied from the Schema Introspection these couple of lines of code in the query definition:

__schema: __Schema!
__type(name: String!): __Type

also I copied the whole Schema of the GraphQL schema introspection system from the same section (in the link above).

this will make sure to avoid disabling graphiql introspection capabilities

I didn't want to modify schema.graphql, as we regularly refetch/overwrite it.

This approach _almost_ worked fine for us:

  1. Add an introspection.graphql file alongside the main schema.graphql file.
  2. Use extend type Query { ... } to add the introspection fields @AlaDouagi mentioned above (thanks!).
  3. Add the other required types.

https://gist.github.com/holmesal/681ecf5ccccf8a14ab29fa5a0f4a8ff9

~This seems to be picked up by the relay compiler just fine.~

Edit: Although the relay compiler picks up introspection.graphql, it seems to assume it's a client-side schema extension (fair). So the only way I've found to get this to work is to use sed to insert the additional introspection fields and types into the original schema.graphql.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scotmatson picture scotmatson  路  3Comments

rayronvictor picture rayronvictor  路  3Comments

sibelius picture sibelius  路  3Comments

johntran picture johntran  路  3Comments

HsuTing picture HsuTing  路  3Comments