Relay: RelayCompiler should validate that queries are named

Created on 25 Apr 2017  路  4Comments  路  Source: facebook/relay

when I add this code relay compiler show errors:

import Relay from "react-relay/classic";
import {graphql, QueryRenderer} from 'react-relay/compat';


export default ({user}) => {
    return <QueryRenderer
        environment={Relay.Store}
        query={graphql`query { viewer { user(id: $userId) { id, username } } }`}
        variables={{userId: user.id}}
        render={({error, props}) => {
            return <div>....</div>;
        }}
    />
};

error:

$ relay-compiler --src ./src --schema ./schema.graphql
HINT: pass --watch to keep watching for 
TypeError: Cannot read property 'value' of undefined

at /project/path/node_modules/relay-compiler/bin/relay-compiler:4656:35
at Array.forEach (native)
at validateTemplate (/project/path/node_modules/relay-compiler/bin/relay-compiler:4655:20)
at TaggedTemplateExpression (/project/path/node_modules/relay-compiler/bin/relay-compiler:4581:12)
at visit (/project/path/node_modules/relay-compiler/bin/relay-compiler:4696:6)
at traverse (/project/path/node_modules/relay-compiler/bin/relay-compiler:4709:8)
at visit (/project/path/node_modules/relay-compiler/bin/relay-compiler:4699:4)
at traverse (/project/path/node_modules/relay-compiler/bin/relay-compiler:4709:8)
at visit (/project/path/node_modules/relay-compiler/bin/relay-compiler:4699:4)
at /project/path/node_modules/relay-compiler/bin/relay-compiler:4713:12
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
bug

Most helpful comment

Reopening to track this: the parser should validate that queries (and fragments, mutations, etc) are named rather than assume that node.name.value is set.

All 4 comments

I found the problem. I should add name to query:

// index.js
graphql`
    query indexQuery($userId: ID!) {
        viewer {
            user(id: $userId) {
                id, username
            }
        }
    }
`;

Reopening to track this: the parser should validate that queries (and fragments, mutations, etc) are named rather than assume that node.name.value is set.

@kassens this seems like something we can do to improve developer experience

I tried this on relay 1.3 and getting an error like:

Parse error: Error: FindGraphQLTags: In module app, a definition of kind OperationDefinition requires a name. in "app.js"

Which isn't quite ideal but better than what you saw probably from an earlier version. Please reopen if you're still seeing this!

Was this page helpful?
0 / 5 - 0 ratings