Giter Site home page Giter Site logo

Comments (4)

dhurlburtusa avatar dhurlburtusa commented on June 11, 2024

@robrichard I need this feature for a project I am working on. So, I have forked the repo and started working on the implementation. However, I ran into a decision issue with the structure of the __arguments field.

Given the following query

query {
  foo {
    bars(isQuux: true, limit: 10, status: "active", sort: ["name", "asc"]) {
      name
      status
    }
  }
}

We would get something like

{
  bars: {
    __arguments: [
      {
        isQuux: {
          kind: 'BooleanValue',
          value: true,
        },
      },
      {
        limit: {
          kind: 'IntValue',
          value: 10,
        },
      },
      {
        status: {
          kind: 'StringValue',
          value: 'active',
        },
      },
      {
        sort: {
          kind: 'ListValue',
          value: ['name', 'asc'],
        },
      },
    ],
    name: {},
    status: {},
  }
}

Now, consider a query like the following where the predicates argument is an object.

query {
  foo {
    bars(predicates: { name: { regex: "^Quu?x" }, status: { eq: "active" } }, limit: 10, sort: ["name", "asc"]) {
      name
      status
    }
  }
}

There are several possible structures that could be returned.

  1. Nested kinds, backwards convention-compatible
{
  bar: {
    __arguments: [
      {
        predicates: {
          kind: 'ObjectValue',
          value: {
            name: {
              kind: 'ObjectValue',
              value: {
                regex: {
                  kind: 'StringValue',
                  value: '^Quu?x',
                }
              },
            },
            status: {
              kind: 'ObjectValue',
              value: {
                eq: {
                  kind: 'StringValue',
                  value: 'active',
                }
              },
            },
          },
        },
      },
      {
        limit: {
          kind: 'IntValue',
          value: 10,
        },
      },
      {
        sort: {
          kind: 'ListValue',
          value: ['name', 'asc'],
        },
      },
    ],
    name: {},
    status: {},
  }
}
  1. Top-level kinds, backwards convention-compatible
{
  bar: {
    __arguments: [
      {
        predicates: {
          kind: 'ObjectValue',
          value: {
            name: {
              regex: '^Quu?x',
            },
            status: {
              eq: 'active',
            },
          },
        },
      },
      {
        limit: {
          kind: 'IntValue',
          value: 10,
        },
      },
      {
        sort: {
          kind: 'ListValue',
          value: ['name', 'asc'],
        },
      },
    ],
    name: {},
    status: {},
  }
}
  1. Simple, not-backwards compatible (good for version 3x). This is the same structure that the args argument (second parameter) has for the resolve function.
{
  bar: {
    __arguments: {
      predicates: {
        name: {
          regex: '^Quu?x',
        },
        status: {
          eq: 'active',
        },
      },
      limit: 10,
      sort: ['name', 'asc'],
    },
    name: {},
    status: {},
  }
}

Which structure would you prefer that I return for version 2? Option 1 or 2?

Should we start version 3 now so that we can use the simple structure?

from graphql-fields.

robrichard avatar robrichard commented on June 11, 2024

@dhurlburtusa I'd prefer to go with No. 1 to keep consistency. I don't think the improvement in No. 3 is worth the churn of breaking changes

from graphql-fields.

dhurlburtusa avatar dhurlburtusa commented on June 11, 2024

@robrichard Thanks for the timely reply.

BTW, did you notice that the items in a ListValue are flattened in version 2 (at least for primitives)? We can't change this for version 2 because that would be a breaking change. (Well, maybe we can by saying it is fixing an unintentional bug.) But Lists with Strings really should be like the following if we want to include kind with each value:

{
  bar: {
    __arguments: [
      # Same as in previous examples
      ...
      {
        sort: {
          kind: 'ListValue',
          value: [
            {
              kind: 'StringValue',
              value: 'name',
            },
            {
              kind: 'StringValue',
              value: 'asc',
            },
          ],
        },
      },
    ],
    ...
  }
}

But the current code simply takes the value.value of the item AST. It does not examine the kind of the items of the List. So, a List of Lists will have undefined for each item since a List AST doesn't have a value property of the value property. It has a values property instead (notice the s at the end), i.e., value.values. Similarly, Object AST doesn't have a value property of the value property. It has a fields property instead, i.e., value.fields.

This is an unreported bug. Would you like me to open an issue for it? How should we handle the fix? Currently, primitive values get flattened and non-primitives become undefined. For backwards compatibility, I guess we can keep primitives as flat instead of an object like { kind: ..., value: ... } but keep them expanded for Lists and Objects. Or we can flatten them when they are items of a list. Which way would you prefer to go?

BTW, wish I could see your talk at JSKongress - 2019 on "Streaming HTTP and GraphQL".

from graphql-fields.

gienec avatar gienec commented on June 11, 2024

This works for ObjectValue type arguments.
I'd say this issue could be closed.

from graphql-fields.

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.