Vincit / objection-graphql

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

Using generated schema with Apollo Express Server? [Solved - yes, it works] #21

Closed Trellian closed 6 years ago

Trellian commented 6 years ago

Hi,

Is it possible to use the generated schema with the Apollo GraphQL Express server? (As per this example https://github.com/Akryum/apollo-server-example)

a direct call to graphql() is working fine:

const IwAddressBaseModel = require('./data/objection/base/IwAddressBaseModel'),
      IwRegionBaseModel = require('./data/objection/base/IwRegionBaseModel');

const graphQlSchema = graphQlBuilder()
                       .model(IwAddressBaseModel)
                       .model(IwRegionBaseModel)
                       .build();

//Execute a GraphQL query.
graphql(graphQlSchema, 
`{
  iwRegions {
    name
  }
}`).then(result => {
  console.warn(JSON.stringify(result, null, 2));
});

const GRAPHQL_PORT = 3001;
const SUBSCRIPTIONS_PATH = '/subscriptions';

var graphQLServer = express();

graphQLServer.use(cors());

graphQLServer.use(bodyParser.urlencoded({ extended: true }));

graphQLServer.use(bodyParser.json());  

graphQLServer.use('/graphql', graphqlExpress({
    graphQlSchema
}));

graphQLServer.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql'
}));

const server = createServer(graphQLServer);

server.listen(GRAPHQL_PORT, () => {
  console.log(`GraphiQL is now running on http://localhost:${GRAPHQL_PORT}/graphiql`)
//  console.log(`GraphQL Subscriptions server is now running on http://localhost:${GRAPHQL_PORT}/${SUBSCRIPTIONS_PATH}`)
});

with console.log showing:

{
  "data": {
    "iwRegions": [
      {
        "name": "Limpopo"
      },
      {
        "name": "Gauteng"
      },
      {
        "name": "Western Cape"
      },
      {
        "name": "Northwest Province"
      },
      {
        "name": "Mpumulanga"
      },
      {
        "name": "Northern Cape"
      },
      {
        "name": "Kwazulu Natal"
      },
      {
        "name": "Garden Route"
      },
      {
        "name": "Eastern Cape"
      }
    ]
  }
}

but when I try to use graphiQL with the same query, it returns an error message:

<body>
<pre>Error: Must provide schema<br> &nbsp; &nbsp;at invariant (C:\xampp\htdocs\iwallqsrv\node_modules\graphql\jsutils\invariant.js:19:11)<br> &nbsp; &nbsp;at Object.validate (C:\xampp\htdocs\iwallqsrv\node_modules\graphql\validation\validate.js:58:37)<br> &nbsp; &nbsp;at doRunQuery (C:\xampp\htdocs\iwallqsrv\node_modules\apollo-server-core\src\runQuery.ts:116:30)<br> &nbsp; &nbsp;at C:\xampp\htdocs\iwallqsrv\node_modules\apollo-server-core\src\runQuery.ts:59:39<br> &nbsp; &nbsp;at &lt;anonymous&gt;<br> &nbsp; &nbsp;at process._tickDomainCallback (internal/process/next_tick.js:228:7)</pre>
</body>
Trellian commented 6 years ago

Again... I gotta stop doing this and wasting your time, sorry.

I got it working as follows:

In the last part of the code above, I changed to the following:

graphQLServer.use('/graphql', graphqlExpress({
    schema: graphQlSchema
}));

graphQLServer.use('/graphiql', graphiqlExpress({
    schema: graphQlSchema,
    endpointURL: '/graphql'
}));

Just posting it here in case it helps someone else

Thanks a million for 'objection-graphql' !