angel-dart-archive / graphql

moved to angel-dart/angel/packages/graphql

Home Page:https://github.com/angel-dart/angel/tree/master/packages/graphql

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting introsection-query result at compile time

venkatd opened this issue · comments

Is it possible to do this?

We are experimenting with some code generation capabilities. Currently we are able to generate types from an introspection query like so:

@GQLGenerator(
    url: SCHEMA_ENDPOINT,
    headerToken: "...",
    namespace: "T")
class TQuery {
  static TRootQueryType fromJson(Map<String, dynamic> json) =>
      TRootQueryType.fromJson(json);
}

It will generate the root query type, all types, fields, enums, docs (from comments), deprecations and so on. (We've got the code out in the public but I wouldn't say it's production ready yet.)

But currently a url to a GraphQL endpoint has to be specified and our code gen will fetch the introspection result from the graphql endpoint.

We are using angel-dart internally, so we'd also like to support the use case of generating the types from an angel-dart graphql server within the same codebase without the extra step of running it as a server.

Would love some guidance on how to accomplish this. Thanks!

You can use the GraphQL class from package:graphql_server/graphql_server.dart, and call parseAndExecute on any query, and the result will be JSON (unless it was a subscription query, in which you'll get a Stream). The GraphQL constructor will automatically perform introspection for you, as well.

At this link there are some queries you can use to request the whole schema via GraphQL:
https://graphqlmastery.com/blog/graphql-introspection-and-introspection-queries

Alternatively, you can open GraphiQL (package:angel_graphql ships a simple handler that embeds this in a page), and look in your devtools to see what query is being sent to the server to request the schema.

You can then dump that information to stdout, etc. without ever having to actually start a server.

P.S. I'm interesting in hearing how you're using Angel! You can optionally share your story here (https://github.com/angel-dart/awesome-angel#companies-using-angel-in-production)

@thosakwe let me clarify our use case.

We are already able to point our code generation prototype to any graphql endpoint and it will generate types based on the introspection query.

So in our code, when we access an api (such as our backend), we can call TRootQuery.fromJson(resp['data']) to get a typed response from the server.

However we're working on a separate use case. We have angel embedded on the client-side and some of our code actually sends requests locally (within a mobile app) to this embedded server.

The reason for this is we have offline-first logic that syncs data to a local database and we're using GraphQL as a common interface between our mobile UI and mobile infrastructure logic. This will allow to experiment with different optimizations without having to change the public interface.

I think the code we're looking for is something like GraphQL(schema).parseAndExecute(introspectionQuery) that should do the trick.

We want to be able to run flutter packages pub run build_runner build so that we can easily regenerate the types from the embedded angel graphql schema. Does that use case make sense? Can clarify further if necessary.

Once we've submitted our app to the app store I would consider it running in production. At that point, I'd be happy to do a brief writeup of how we're using angel-dart :)

Sorry, went out of town for several days. I'm a little bit confused. I believe that your use case should already be possible. The only thing is that you might not necessarily be able to use build_runner for it.

I should have clarified. The use case is already possible. I was just explaining the reason behind the original request in case you had feedback on the approach.

Currently we can generate types from:

@GQLGenerator(
    url: SCHEMA_ENDPOINT,
    headerToken: "...",
    namespace: "T")
class TQuery {
  static TRootQueryType fromJson(Map<String, dynamic> json) =>
      TRootQueryType.fromJson(json);
}

But we are working on:

@GQLGenerator(
    schema: angelDartSchema,
    headerToken: "...",
    namespace: "T")
class TQuery {
  static TRootQueryType fromJson(Map<String, dynamic> json) =>
      TRootQueryType.fromJson(json);
}

No changes are needed from angel-dart to support this :)