How to Query All the Graphql Type Fields Without Writing a Long Query

How to query all the GraphQL type fields without writing a long query?

Unfortunately what you'd like to do is not possible. GraphQL requires you to be explicit about specifying which fields you would like returned from your query.

How to define schema in graphql query for unknown or dynamic response

In general you should avoid "generic" key-value pairs in your graphql schema, because you than loose the typesafety. I think this is one of the reasons, why there is no Map type in GraphQL.

In your case, you may could use a union type for your result instead.

If you really need something more generic, you could use an array with a tuple-like object inside, like so:

type Tuple {
name: String!
value: String!
}

type StudentDetails {
# ...
additionalValues: [Tuple!]
}

(See also this discussion: https://github.com/graphql/graphql-spec/issues/101#issuecomment-170170967.)

Another option: specify all possible returned fields from all different student types and mark them as optional (nullable):

type StudentResponse {
# common for ALL students (required, non-nullable)
chemistry: String!
maths: String!

# optional, returned only for some students
language: String
physics: String
}


Hope that helps!



Related Topics



Leave a reply



Submit