Skip to content
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

Tapioca compiler #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

iMacTia
Copy link

@iMacTia iMacTia commented Jan 29, 2024

Summary

Adds a tapioca compiler to automatically generate RBIs for dynamically generated GraphQL type classes.

How it works

The compiler is opt-in, as it relies on a Types constant being defined with all the GraphQL::Client.types.
To make it pick up and generate RBIs for your client, you just need to set this constant in your API module:

module SWAPI
  # Configure GraphQL endpoint using the basic HTTP network adapter.
  HTTP = GraphQL::Client::HTTP.new("https://example.com/graphql") do
    ...
  end  

  Schema = GraphQL::Client.load_schema(HTTP)
  Client = GraphQL::Client.new(schema: Schema, execute: HTTP)

  # This is what triggers the compiler discovery of the type classes and their RBI generation
  const_set(:Types, Client.types)
end

Once that is added and tapioca dsl is run, RBIs for SWAPI::Types::<name> will be generated, allowing you to use them in Sorbet signatures. Obviously, the Types constant can be called anything you'd like, as far as its value it set to Client.types.

@rmosolgo
Copy link
Collaborator

Hey, thanks for bringing this over from graphql-client! I'm definitely game to merge this and release it. But, could you add some tests for it to be sure that we don't accidentally break it in the future? For example, I could imagine a test that creates a new schema (perhaps based on a SDL definition?) and generates the the RBI file for it, then compares the generated file to our expected content. (That's what came to mind for me but I'm open to any kind of testing.) What do you think of adding something like that?

@themagickoala
Copy link

What's the status of this @iMacTia? We're looking to add graphql-client into our application, but not having static types is kind of a deal-breaker for us

@iMacTia
Copy link
Author

iMacTia commented Nov 7, 2024

Sorry for the late reply @themagickoala, unfortunately I didn't have any time to add tests to this, I'm really sorry!
We've been using this in production for a while so feel free to add the compiler to your project in the meantime.
I really hope one day I'll find the time to finish this up 😄!
(But if anyone else would like to jump in and add tests on my behalf, I won't complain!)

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.

3 participants