Documenting GraphQL short-hand model - graphql

How does one document the model, using GraphQL shorthand notation?
type User {
name: String
}
Say I want to add a description for the type/attribute above.

Descriptions come in the form of comments:
# Someone who uses something
type User {
# How we refer to the user
name: String
}
Those should be added as descriptions to the schema generated from this notation.

Related

Query GraphQL with multiple types

I'm trying to make a GraphQL query, out of the ordinary, but so far I haven't found anything related.
I would like to leave the where parameter with two optional types, see the example. Is this supported?
input Person {
name: String
email: String
}
input Company {
name: String
domain: String
}
type GetSearch {
name
email
domain
}
extend type Query {
getSearch(where: Person | Company): [GetSearch]
}
This is not possible.
Adding Input Union type to GraphQL was already proposed back in 2018, but it was never added. See this discussion for more details.

GraphQL schema - is it possible to set constraints on a custom type field

Here is a simplified snippet out of a GraphQL Schema I work with. There are - an enum, type Contributor and type Review.
enum CONTRIBUTOR_TYPE {
AUTHOR
EDITOR
}
type Contributor {
name: String
role: CONTRIBUTOR_TYPE
}
type Article {
text: String
authors: [Contributor]
}
What I want to describe, is that the "authors" field is not just array of type Contributor, but Contributor with specific job role. In pseudocode it would look like
authors: [Contributor{type: CONTRIBUTOR_TYPE.AUTHOR}]!
Is there a way to describe this constraint in GraphQL Schema?
You should be able to use schema directives to describe / enforce the constraint. For apollo server check out https://www.apollographql.com/docs/apollo-server/schema/creating-directives/, for graphql-java check out https://www.graphql-java.com/documentation/v15/sdl-directives/, for other server implementations check their corresponding documentation.

what's difference between schema and documents in Graphql?

what's the difference between schema and documents in Graphql?
schema is like this:
type Query {
fo: String
}
but the document is like:
query SomeQuery {
foo {
bar
}
}
the spec is really confusing https://graphql.github.io/graphql-spec/June2018/#sec-Language.Document
I always use schema but for client-side type generation in graphql-code-generator it needs document file. https://graphql-code-generator.com/docs/getting-started/documents-field
A document is really any string containing valid GraphQL syntax. According to the spec, a document contains one or more definitions, where a definition could be:
an operation definition
query UsersQuery {
users {
id
email
}
}
a fragment definition
fragment UserFragment on User {
id
email
}
a type system definition
type User {
id: ID!
email: String!
}
a type system extension
extend type User {
name: String
}
Operation and fragment definitions are known as executable definitions. Documents sent to a GraphQL service must only contain executable definitions. Type system definitions and extensions are used in describing a schema -- that's why we commonly call them Schema Definition Language (SDL). A schema is a GraphQL service's "collective type system capabilities" -- it's basically a collection of types and directives that represent everything your GraphQL service can do.
A schema may be described using type system definitions, but it's not really accurate to say that the type definitions are the schema because the schema itself also includes the actual field resolution logic as well.

Can an enum return description (string) in GraphQL?

I'm struggling with my GraphQL API because I need to take advantage of Enums while keeping their full description in the frontend.
In short:
I have different states for a product: "Has been sent", "Not sent yet", "Received".
So this is the right place to use Enums:
enum ProductState {
HAS_BEEN_SENT
NOT_SENT_YET
RECEIVED
}
But I need to display the proper strings on the frontend ("Has been sent", and not "HAS_BEEN_SENT").
I can't use a simple solution as "replace underscores with spaces and lowercase the string" because my API is not in English but in French (so I have accents and special characters).
Can't an Enum return a string? Or an object?
I tried with directives but impossible to get it work...
Actually I don't care how it is written in the database (the uppercase or lowercase form) nor in the GraphQL API. I just need my client to access to the different product states in their "French" form.
As far as I know there is only one supported syntax for defining enums in GRAPHQL, which is the one you are using. You cannot associate an enum with a string value.
#NOTE: I would probably use a more descriptive name as opposed to ProductState
enum AllowedProductStatus {
HAS_BEEN_SENT
NOT_SENT_YET
RECEIVED
}
However, if you use apollo server you can use resolvers to add custom values.
const resolvers = {
AllowedProductStatus: {
HAS_BEEN_SENT: "Has been sent",
NOT_SENT_YET: "Not sent yet",
RECEIVED: "Received"
}
};
Alternatively, If you simply wants to make it unique you could also use the directive #unique
type Product {
status: String! #unique
}
Hope it was helpful.

Convert GraphQL shorthand notation to respective object?

I am working on a GraphQL server setup where it can parse Types passed into it from strings, and I am looking for a solution to convert a string to an appropriate object. For example, if the string here is passed in:
type User { id: String, name: String }
My function would return the equivelant of running this code:
new graphql.GraphQLObjectType({
name: 'User',
fields: {
id: { type: graphql.GraphQLString },
name: { type: graphql.GraphQLString },
}
});
The key here is to be agnostic, so I could also pass in say, interfaces, and other shorthand and have it return the appropriate object. I have gotten as far as achieving the Abstract Syntax Tree from the graphql/language module bu using graphql_language.parse(str) function, but I'm unsure where to go from here.
The reference GraphQL-JS implementation on GitHub already has a function, buildASTSchema, that takes a parsed type schema and creates a set of JavaScript objects. So the best way to see how to do it would be to consult that source code on GitHub: https://github.com/graphql/graphql-js/blob/master/src/utilities/buildASTSchema.js
Alternatively, perhaps your tool can be built using that function. Since that repository is maintained by the core GraphQL team, you can be pretty confident that it will be up to date with new additions to the spec.
Edit from comment: If what you're trying to do is generate an executable GraphQL schema/server from the type language string, then you can use the generateSchema function from the graphql-tools package, as documented here: http://docs.apollostack.com/apollo-server/generate-schema.html

Resources