Coder Social home page Coder Social logo

Comments (7)

benjaminjkraft avatar benjaminjkraft commented on June 21, 2024

Another related problem: you might want to be able to apply omitempty to a particular, or all, fields of the input object. Right now it only applies to arguments. This will actually require new code to handle!

from genqlient.

csilvers avatar csilvers commented on June 21, 2024

This is, somewhat surprisingly, turning out to be a non-trivial barrier to genqlient adoption.

I thought of another suggestion, that maybe avoids some of the problems above: allow a user to specify the input fields they're interested in much like (in graphql) they specify the output fields. The code could be something like this:


func QueryMyOperation(...) {
   _ = `# @genqlient(inputFor: "MyOperationName")
   type MyOperationInput {
      email
      # @genqlient(pointer: true)
      validate
   }
   `
   _ = `#genqlient
  query MyOperationName($input: MyOperationInput) {
     user(input: $input) { id }
  `
  genqlient.MyOperationName(...)

Then genqlient would validate that the inputFor is a subset of the actual graphql type MyOperationInput, and that it includes all non-optional fields -- and would generate its own type for this, called MyOperationNameMyOperationInput or something. And that own type would follow the pointer/omitempty/etc directives. And then you could use that type name as an input to QueryMyOperation, or construct it inside QueryMyOperation, or whatever.

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on June 21, 2024

Oh, that's a neat idea! I'll take a look at how to make it work.

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on June 21, 2024

As I mention in the above-linked committ, the new bindings option is also a sort of workaround to this.

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on June 21, 2024

Ugh, I think this is still worth doing, but the syntax is proving to be a mess.

I like Craig's idea above -- which I would modify to saying

fragment InputForMyQuery on MyInput {
  email
  # @genqlient(pointer: true)
  validate
}

# @genqlient(inputs: ["InputForMyQuery"])
query MyQuery(input: $MyInput) { ... }

That has the nice property that it parses, but sadly it doesn't validate (GraphQL doesn't allow fragments on input types, because where would you us them?). We could just disable that validation rule (replacing it with our own less restrictive one, perhaps)? Or we could say you specify it in a comment, like the @genqlient directive itself, but that gets messy because GraphQL has no block-comment syntax. (As does having it be an argument to the genqlient directive, because then it still all needs to be commented.)

Probably the least-bad is to just disable the validator. I don't like diverging from standard GraphQL syntax, but I think it's better than the alterrnatives; if we later find a better way we can add it as an alternative.

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on June 21, 2024

An amusing bug: this means it's not possible to use the following type in genqlient (you need it to be a pointer):

input RecursiveInput { rec: RecursiveInput }

from genqlient.

benjaminjkraft avatar benjaminjkraft commented on June 21, 2024

Ok, I've put this off for too long, it's time to just choose the least-bad option.

I think the least-bad option in my mind is basically a variant of my original idea, namely:

# @genqlient(for: "MyInput.myField", <options>)
# <maybe more of these to apply to several fields>

If we come up with a better syntax later it shouldn't be hard to support it as an alternative. It'll be a little work to add support for repeated directives, but I think still easier than trying to get validation right for a fragment-like syntax, and it's a lot less ugly than doing pointerfields: <list>.

We will have to say that your directives must match exactly for all queries with the same inputs, unless you also use typename (this applies too all the approaches except fragments which would effectively require typename).

from genqlient.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.