0

I am building a graphql server on expressjs. Below is the code:

const express = require('express');
const app = express();
const {ApolloServer} = require('apollo-server-express');

const server = new ApolloServer({schema});
server.applyMiddleware({app, path: '/graphql'});

app.listen(4000,()=>console.log(`server started on port $4000}`));

Here is my schema:

const typeDefs = `

    input CustomersInput {
        EMAIL_ADDRESS: String
        NAME: String
        HOME_PHONE: String
        SPA_FOLIO_ID: ID
        ALL_CUSTOMER_ID: ID
    }

    type Customer {
        ALL_CUSTOMER_ID: ID
        NAME: String
        ALL_CUSTOMER_TYPE: String
        FIRST_NAME: String
    }

    type Query {
        customers(input: CustomersInput): [Customer]!
    }

    schema {
        query: Query
    }
`;

const resolvers = {
    Query: {
        customers(parent, args, ctx, resolveInfo) {
            return joinMonster.default(resolveInfo,ctx, async sql=>{
                console.log(sql)
                return knex.raw(sql); 
            });
        },
    },
}

const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
});

joinMonsterAdapt(schema, {
    Query: {
        fields: {
            customers: {
                where: (customerTable,args) => {
                    return escape(`${customerTable}.UPPER_FIRST_NAME || ' ' || ${customerTable}.UPPER_LAST_NAME || ' ' || ${customerTable}.UPPER_FIRST_NAME like %L`, `%${args.input.NAME.toUpperCase()}%`);
                },
            },
        }
    },
    Customer: {
        sqlTable: 'ALL_CUSTOMER',
        uniqueKey: 'ALL_CUSTOMER_ID',
    },
});



module.exports = schema;

When I run the app, and go to http://localhost:4000/graphql, and use the query:

{
  customers(input:{NAME: "as"}){
    FIRST_NAME
    ALL_CUSTOMER_ID

  }
}

I get back:

{
  "data": {
    "customers": [
      {
        "FIRST_NAME": null,
        "ALL_CUSTOMER_ID": "563",

      },
    ]
  }
}

This is happening because when I look at the sql query which joinmonster is generating, it is only requesting for customer id and nothing else as seen below:

SELECT
  "customers"."ALL_CUSTOMER_ID" AS "ALL_CUSTOMER_ID"
FROM ALL_CUSTOMER "customers"
WHERE "customers".UPPER_FIRST_NAME || ' ' || "customers".UPPER_LAST_NAME || ' ' || "customers".UPPER_FIRST_NAME like '%AS%'

When I run the exact same code but use express-graphql instead,

const expressGraphQL = require('express-graphql');

app.use('/graphql', expressGraphQL({
        schema,
        graphiql: true
    }))

This is the query which join monster is generating:

SELECT
  "customers"."ALL_CUSTOMER_ID" AS "ALL_CUSTOMER_ID",
  "customers"."FIRST_NAME" AS "FIRST_NAME"
FROM ALL_CUSTOMER "customers"
WHERE "customers".UPPER_FIRST_NAME || ' ' || "customers".UPPER_LAST_NAME || ' ' || "customers".UPPER_FIRST_NAME like '%AS%'

And everything works as expected. Am I missing something?

abhirham
  • 151
  • 2
  • 12

1 Answers1

0

Did you find a solution to this issue? I'm seeing the same thing with null values being returned except for the id in my types. I was using express-graphql and everything works fine like you describe. When I swap it out for ApolloServer from apollo-server-express, I'm able to see that the schema seems to load fine and display in the GraphQL playground, but the sql queries aren't executing normally, causing null values every where.

In the latest version of join-monster (3.0, unreleased), they have moved all of the join-monster specific syntax in the extensions property. I'm thinking this may fix the issue, if it is the extra non-standard GraphQL properties added to the types that is causing it.

const User = new GraphQLObjectType({
  name: 'User',
  extensions: {
    joinMonster: {
      sqlTable: 'accounts', // the SQL table for this object type is called "accounts"
      uniqueKey: 'id' // id is different for every row
    }
  },
  fields: () => ({
    /*...*/
  })
})