Skip to content

Function-like macro and shared fragments#568

Open
RedHatter wants to merge 3 commits into
graphql-rust:mainfrom
RedHatter:shared-fragments
Open

Function-like macro and shared fragments#568
RedHatter wants to merge 3 commits into
graphql-rust:mainfrom
RedHatter:shared-fragments

Conversation

@RedHatter

Copy link
Copy Markdown

Fixes issue #312.

Function-like macro

This PR adds the graphql_queries macro which generates code for all the operations in a single graphql file. It uses the same syntax and attributes as the derive macro as well as a new module_visibility attribute. This allows the attribute parsing code to be reused but if a different syntax is preferred I would be happy to change it.

graphql_queries!(
    query_path = "query.graphql",
    schema_path = "schema.graphql",
);

Shared fragments

When more than one operation is being generated scalars, fragments, and enums are now split out into a separate common module. This allows those types to be reused between operations in the same file and generated at the same time.

The name of the common module is something to consider here. I called it common however shared would work too and something like graphql_common / graphql_shared would have the advantage of being less likely to conflict with a user defined symbol.

If the graphql_queries macro is used multiple times in the same file the name of the common module name will conflict but this can easily be worked around.

mod a {
  graphql_queries!(
      query_path = "a.graphql",
      schema_path = "a.graphql",
  );
}

mod b {
  graphql_queries!(
      query_path = "b.graphql",
      schema_path = "b.graphql",
  );
}

If this is a worry I could add a new attribute for setting a custom name for the common module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant