Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 34 forks source link

__typename being sent to SQL backend as part of a query? #24

Closed Trellian closed 6 years ago

Trellian commented 6 years ago

Hi @koskimas,

I'm experiencing a problem with __typename appearing in the final query string being sent to the SQL engine, I'm hoping you can help here. Have you seen anything like this before, or could you point me in a direction that might help?

I'm using Apollo Server Express 1.2.0 and Apollo Client 2.0.2.

I have a simple query that works fine in GraphiQL, but not when sent via the client.

{ 
  iwRegions {
    country_id
    name
  }
}

and I'm getting an error back as follows:

message: "select `iw_region`.`country_id`, `iw_region`.`name`, `__typename` from `iw_region` - ER_BAD_FIELD_ERROR: Unknown column '__typename' in 'field list'"

I've tried raising issues on the GitHub for apollo-client, apollo-server and vue-apollo, but I've not had any response there :(

Any help you can give would be greatly appreciated!

Thank you Adrian

koskimas commented 6 years ago

Hi

Here's the code that generates the select clause. Try debugging that. I have no idea why __typename appears in the AST node.

Trellian commented 6 years ago

Thanks for the quick response!!! I did try that, using:

graphQLServer.use('/graphql', graphqlExpress({
  schema: graphQlSchema,
  formatParams: (params) => {
    const { query, ...rest } = params;
    const newQuery = query.split('__typename\n').join('')
    console.warn('Query: ' + JSON.stringify(newQuery, null, 2))
    return {
      query: newQuery,
      ...rest
    }
  }
}));

but then it just comes back with a lot of errors complaining about missing __typenames...

Trellian commented 6 years ago

whoops sorry I replied on the wrong forum!

koskimas commented 6 years ago

http://graphql.org/learn/queries/#meta-fields

It seems that objection-graphql needs to exclude that property.

Trellian commented 6 years ago

That did it!

I modified in _filterForSelects() (line 381:

      builder.select(selects.map(it => {
      const col = modelClass.propertyNameToColumnName(it);

        if (jsonSchema.properties[it]) {
          return `${builder.tableRefFor(modelClass)}.${col}`;
        } else {
          return col;
        }
      }));

to

      builder.select(selects.filter(it => {
          return (it !== '__typename');
      }).map(it => {
      const col = modelClass.propertyNameToColumnName(it);

        if (jsonSchema.properties[it]) {
          return `${builder.tableRefFor(modelClass)}.${col}`;
        } else {
          return col;
        }
      }));

Thank you! I just needed a prompt in the right direction.

koskimas commented 6 years ago

This is now fixed in master. I'll release a new patch version soon.

Trellian commented 6 years ago

Ha! you beat me to the PR, and I only took 3 minutes! I've never had an author patch an issue that fast on GitHub before, Kudos to you!