-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Write GraphQL Schema to a File #3286
Comments
I have a couple of concerns here.
Additionally, if we are to write the schema to a file, we should be eyeing to read it at the runtime and load the in-memory schema instead of writing the whole schema as an annotation. We should add that as a future plan. In that case, users may not be able to disable the file creation. (Or maybe we can create a temp file and delete it once we load the schema, we can decide later). @shafreenAnfar any thoughts? P.S.: I think we can come up with a better name than |
Good concerns. In the case of
I think we can do the same for gql as follows,
The full set of configuration is as follows,
|
I did not understand this configuration. Do we need to provide it when we running the service or building? |
No it is a separate command, it is not part of the build command. Check the below link. |
Right. This looks good. But in this case, the user has to execute a separate command to generate the file, right? |
Alternative approach: We can introduce two configurable variables inside the GraphQL module to get the inputs for enabling or disabling the schema file generation and the file name. Ex: Configurable variables: public configurable boolean schemaFileGenEnabled = true;
public configurable string schemaFileName = ""; Execution command: bal run -- -Cballerina.graphql.schemaFileGenEnabled=true -Cballerina.graphql.schemaFileName=fileName @shafreenAnfar @ThisaruGuruge any thoughts? |
@DimuthuMadushan If we can have an API in the GraphQL compiler plugin that returns a schema string that can be directly written to a file, and if there's a way to access that API from the graphql tool, I think we can resolve this. The configurable variable approach looks good. If the consistency across the different tools isn't much of a concern, I'd prefer that option. @shafreenAnfar any thoughts? |
As discussed offline, due to the following reasons, decided to remove the schema file generation configs and generate the file always.
The proposal is updated according to the changes. |
To be consistent with the other implementation(e.g., openApi), the GraphQL schema file generation is moved to the CLI side. The proposal is updated with the new changes. |
To clarify the behavior. Once we completely implement this, to write the GraphQL schema to a file, we have two options:
If this is the case, what was the reason behind having two different approaches? If this is how OpenAPI tool handles the OpenAPI spec generation, what was the reason behind the decision? @lnash94 thoughts? |
@ThisaruGuruge yeah we added the flag with the requirements.
|
Summary
The Ballerina GraphQL package generates the schema for a given Ballerina GraphQL service at the compile time. Currently, the generated schema is exposed to the outside only via introspection. This proposal is to write the generated schema to a file in GraphQL Schema Definition Language(SDL) that the users can access.
Goals
Motivation
Since the Ballerina GraphQL module uses the code-first approach, users can not have the generated schema from the service in Schema Definition Language(SDL). The only way to check the schema in SDL is by using a client tool (e.g., GraphQL Playground). Therefore, it’s essential to have a way to access the schema generated from the service. With this, the users will be able to access the schema and ensure the validity of the generated schema. Also, the file can be exported and used with any other GraphQL libraries without any issue since the schema is written in a language-agnostic way.
Description
The SDL schema file generation can be done using the GraphQL CLI command. The GraphQL CLI tool gets the compilation of given GraphQL service to access the schema string. It extracts the annotated schema string from the syntax tree and deserializes it to the schema object. This schema object is used to generate the GraphQL SDL schema string using the
SdlSchemaStringGenerator
which is published in the GraphQL Commons package. The generated SDL schema string is written to a file and exported into the given path in the CLI command. When an error occurs during the SDL schema generation, a stderr message is logged in the console. The errors that don't have a specific location return (-1:-1,-1:-1) as the error location.SDL schema string generator
The SDL Schema String Generator is a new component added to the GraphQL Commons package. It accepts the generated schema object and creates the schema string in GraphQL Schema Definition Language.
CLI command
The CLI command for the SDL file generation includes multiple parameters as follows.
1. graphql-service-file-path
The
ballerina-service-file-path
parameter specifies the path of the ballerina service file (e.g.,graphql.bal
) and is mandatory. If there are multiple GraphQL services included in thebal
file, the command generates the schema for each graphql service.2. output-location
The SDL schema file is exported into the location given as the
output-location
in the CLI command. This is an optional parameter. By default, the schema file is exported into the same directory where thebal
command is executed.3. service-base-path
The
service-base-path
can be used to specify the absolute service path of the GraphQL service(e.g.,/gql
). Then the schema is generated only for the service that has the matching absolute path. If no matching service is found, an error is returned. This is an optional parameter as theoutput-location
.Schema File
The schema file is written in GraphQL Schema Definition Language(SDL) with a name of the following format. The file extension is
.graphql
.The
service-base-path
is a concatenation of base path segments by an underscore(e.g., “graphql_service”). Theduplicate-count
is appended to the name in order to distinguish the schema file when having multiple services with the same base path in a given service file. It is an optional segment in the file name.Also, if there is a duplicate schema file in the output directory, a response message is displayed in the console to get a user input. It asks whether to override the existing file or generate a new file. Based on the user input, It decides whether to override the file or create a new file. If the user doesn’t want to override it, the
duplicate-count
is appended to the name of the newly generated schema file.The text was updated successfully, but these errors were encountered: