cynic icon indicating copy to clipboard operation
cynic copied to clipboard

Support build time introspection queries

Open obmarg opened this issue 4 years ago • 10 comments

For some use cases, it might make sense to query for a schema at build time from a running server. Should think about supporting those use cases.

obmarg avatar Jul 05 '20 22:07 obmarg

introspection?

jxnu-liguobin avatar Mar 04 '21 10:03 jxnu-liguobin

@jxnu-liguobin graphql servers support "introspection queries": https://graphql.org/learn/introspection/ which let you ask a server about it's schema. You can build up a schema from these, which cynic could use instead of a schema on the filesystem like it uses currently.

Although cynic would first need to support introspection queries, as it doesn't currently.

obmarg avatar Mar 04 '21 12:03 obmarg

@obmarg Yes, I'll think about it and see if I can make it. I think we can continue to use schema_ path property to set the introspection server address.

jxnu-liguobin avatar Mar 04 '21 13:03 jxnu-liguobin

@obmarg Hi, So, what I want to do is to implement a similar function load_schema_from_server?

image

Here, suppose we use the HTTP URL setting schema_path, every derive macro needs to read schema_path when using introspection, it may need to add a layer of cache here to avoid the acquisition failure and efficiency problems.

jxnu-liguobin avatar Mar 05 '21 03:03 jxnu-liguobin

Yeah, that is roughly what you'd want.

Indeed a cache would be good - would definitely like to avoid running an introspection query for every single derive.

obmarg avatar Mar 05 '21 10:03 obmarg

@obmarg Since introspection itself is also a graphql, we need graphql query, and we may need a config similar to schema_path. How to configure this? Or the schema_path change path to a struct(I'm not sure I can do that). (the introspection results can refer to the introspection decoding implementation of graphql-client, or directly introduce it .(it's really not good))

jxnu-liguobin avatar Mar 10 '21 11:03 jxnu-liguobin

Sorry for the delay getting back to you @jxnu-liguobin - I've not had any time to pay attention to cynic the past couple of weeks.

You're correct that we will need an introspection query implementation to do this. I've got a WIP cynic implementation of one sitting on my machine (I was looking into doing this a couple of months ago, but got distracted by other bits). I've uploaded it to this branch if you want to have a look. I can't remember what needed fixed in it - it may be easy or may be some missing feature in cynic that we need in order to finish it off.

As for how to configure this: I am not entirely sure. Given that we probably don't want to make a request for every derive and GraphQL servers can sometimes require authentication or other configuration I'm not sure it even makes sense to update the derives to make an introspection query. The number of options we'd need to support is quite large, and caching might be difficult.

Maybe it makes more sense to provide users the ability to run an introspection query and output a schema.graphql file from that. Then users can write a build.rs or similar to fetch their query using the functionality we provide, and the derives can just read the schema.graphql that outputs. Seems like it might be a lot easier to implement things that way.

obmarg avatar Mar 21 '21 15:03 obmarg

Maybe it makes more sense to provide users the ability to run an introspection query and output a schema.graphql file from that. Then users can write a build.rs or similar to fetch their query using the functionality we provide, and the derives can just read the schema.graphql that outputs. Seems like it might be a lot easier to implement things that way.

Yes, it will be easier.

You're correct that we will need an introspection query implementation to do this. I've got a WIP cynic implementation of one sitting on my machine (I was looking into doing this a couple of months ago, but got distracted by other bits). I've uploaded it to this branch if you want to have a look. I can't remember what needed fixed in it - it may be easy or may be some missing feature in cynic that we need in order to finish it off.

It looks like only the structure is defined, which looks fine if the user writes the low-level API himself(users need write build.rs to executes graphql query).

jxnu-liguobin avatar Mar 22 '21 02:03 jxnu-liguobin

It looks like only the structure is defined, which looks fine if the user writes the low-level API himself(users need write build.rs to executes graphql query).

Yeah, I only got as far as writing the structure of an introspection query. We'll also need to write the code that converts the results of an introspection query into a schema.

I'd probably let users write the actual HTTP call & output of the schema themselves. It's not too hard to do, and there's a fairly wide variety of ways to do it: different HTTP clients, different authentication schemes etc. so be quite hard to cover all the possibilities. Can provide some examples in the repository that users can use as a starting point.

obmarg avatar Mar 22 '21 11:03 obmarg

👌

jxnu-liguobin avatar Mar 22 '21 11:03 jxnu-liguobin

Between cynic-introspection & cynic-cli this is now supported. It's not fully documented, but I wouldn't recommend this approach for most users anyway.

A smart user who wants to go against my recommendation should hopefully be able to figure it out for themselves.

obmarg avatar Aug 05 '23 17:08 obmarg