Giter Site home page Giter Site logo

Comments (11)

josephsavona avatar josephsavona commented on May 7, 2024

The main reason we didn't support input objects right off the bat is that it's possible for them to contain nested variables, which complicates processing of arguments at runtime. It isn't too difficult to add support for them, but the simple workaround is a variable as you suggested.

Example nested variable:

  ...
  credits(orderBy: {field: "name", order: $order}) {...}

from relay.

fson avatar fson commented on May 7, 2024

Thanks for the explanation @josephsavona!

A quick follow up question: I changed this to a variable like this:

module.exports = Relay.createContainer(Movie, {
  initialVariables: {
    orderBy: { field: 'name', order: 'DESC' },
  },
  fragments: {
    movie: () => Relay.QL`
      fragment on Movie {
        id,
        credits(first: 10, orderBy: $orderBy) {
          edges {
            node {
              name
            }
          }
        }
      }
    `,
  },
});

Now I'm getting an error on the server: Argument "orderBy" expected type "ReindexOrderBy" but got: {field: "name", order: "ASC"}. (That is, the schema expects an enum ASC but Relay printed it as a string.)
Is printRelayQuery not printing enums inside input objects a known bug or should I open a separate issue about that?

from relay.

josephsavona avatar josephsavona commented on May 7, 2024

This is a known limitation - we didn't have time to add support for printing enums inside input objects. Again, this isn't difficult but it requires adding quite a bit more metadata about arguments in order to print them correctly.

cc @leebyron @dschafer is there anything we can do to make it easier for tools to autogenerate valid inputs?

from relay.

fson avatar fson commented on May 7, 2024

@josephsavona I noticed the enum values sent separately in the request body using the variables property would already work. When does Relay send variables separately and when does it bake them in the query string?

I wonder if I could somehow force them to be sent separately and use this to work around the fact that enums can't be printed. If not, I'll just try to avoid enums in input objects for now.

from relay.

josephsavona avatar josephsavona commented on May 7, 2024

Relay inlines all variables in queries in order to allow the same fragment to be used with different values. The variables HTTP query param ends up being ignored (because no variables appear in the query) - they're only useful for debugging what the root variables were.

For an example of why inlining is necessary, consider the ProfilePicture component. It might appear in multiple places in the application hierarchy, each referencing the same $size variable, but with different values:

// Parent component
foo: () => Relay.QL`
  fragment on Story {
    author {
      ${ProfilePicture.getFragment('photo', {size: 64}),  # large photo for author
    },
    comments {
      ...
         ${ProfilePicture.getFragment('photo', {size: 32}),  # small photo for commenters
  }
`
// ProfilePicture:
photo: () => Relay.QL`fragment on User { 
  profilePicture(size: $size) {...}
}

The solution for enums within object values is to construct a new variable for every input object, so that it can be sent as plain JSON instead of an inline value.

from relay.

fson avatar fson commented on May 7, 2024

Thanks for the explanation @josephsavona. That makes sense.

Just making sure I understood the proposed solution for supporting enums within object values, if we were to implement that the fragments and variables sent to the server would be something like this?

fragment _efda7a5a on Story {
  author {
    ..._e8b7be43
  }
  comments {
    ...
        ..._71beeff9
  }
}
fragment _e8b7be43 on User {
  profilePicture(size: $size_6b9df6f) {...}
}
fragment _71beeff9 on User {
  profilePicture(size: $size_98dd4acc) {...}
}

variables (with generated unique names):

{
  "size_6b9df6f": 64,
  "size_98dd4acc": 32
}

from relay.

josephsavona avatar josephsavona commented on May 7, 2024

Heads up that I'll send a PR for this soon.

from relay.

josephsavona avatar josephsavona commented on May 7, 2024

I've started adding support here - https://github.com/josephsavona/relay/commits/inputobjects - but this will also require some upstream changes. printRelayQuery needs to return {text, variables} instead of the text alone.

from relay.

fson avatar fson commented on May 7, 2024

@josephsavona Looks good so far!

Noticed printRelayQuery is in __forks__. What is the "canonical" repo for it?

from relay.

clentfort avatar clentfort commented on May 7, 2024

Correct me if I'm wrong: __forks__ contains modules that differ from facebooks internal version of relay. Since facebook does not yet use the RFC version of GraphQL internally they need a different printer for their queries.

from relay.

josephsavona avatar josephsavona commented on May 7, 2024

Yup, that's is. This is the same Relay we use internally and the forks account for the (minor) differences in GraphQL versions.

from relay.

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.