Unable to perform sorting using Apollo server with Dgraph

I’m trying to sort the date by timestamp, but I’m unable to perform the same using the resources given in the Dgraph docs.

Used below schema

type Post {
    id: ID!
    title: String!
    timestamp: String!
    username:	UserName
    isPrivate: Boolean!
    avatar: String
    body: String!
    images: [Image]
    commentsVisibility:	Boolean!
    isReplyEnabled:	Boolean!
    comments:	[Comment]
    tags:	[Tag]
    first: Int
    offset: Int
    hashtag: [String]
}

enum SortOrder {
    ASC
    DESC
}

enum SortableField {
    title
    timestamp
} 

input Sort {
    field:SortableField,
    order:SortOrder = DESC
}

type AllPostResponse {
    success: Boolean!
    message: String
    token: String
    data: [Post]
}

Query:

getAllPost(sort: Sort,first: Int, offset: Int): AllPostResponse

Getting below mentioned error:

Error:

/home/yuvaraj/node_modules/graphql/language/lexer.js:360
    throw (0, _syntaxError.syntaxError)(source, pos, unexpectedCharacterMessage(code));
    ^

GraphQLError [Object]: Syntax Error: Cannot parse the unexpected character "\u00A0".
    at syntaxError (/home/yuvaraj/node_modules/graphql/error/syntaxError.js:15:10)
    at readToken (/home/yuvaraj/node_modules/graphql/language/lexer.js:360:40)
    at Lexer.lookahead (/home/yuvaraj/node_modules/graphql/language/lexer.js:75:108)
    at Lexer.advance (/home/yuvaraj/node_modules/graphql/language/lexer.js:58:35)
    at Parser.expectOptionalToken (/home/yuvaraj/node_modules/graphql/language/parser.js:1425:19)
    at Parser.optionalMany (/home/yuvaraj/node_modules/graphql/language/parser.js:1499:14)
    at Parser.parseInputFieldsDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:1065:17)
    at Parser.parseInputObjectTypeDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:1049:23)
    at Parser.parseTypeSystemDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:720:23)
    at Parser.parseDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:150:23) {
  locations: [ { line: 121, column: 1 } ]
}

Kindly help me out to fix this issue ASAP.

Thanks & regards
Yuvaraj

Hi Team, Can I get reply for the above issue.

Can you please share the exact, full query you tried? Also, please share the full schema.
Please also let us know which version of Dgraph you are using.

Please find the details.
Schema:

type Post {
id: ID!
title: String!
timestamp: String!
isPrivate: Boolean!
avatar: String
body: String!
first: Int
offset: Int
hashtag: [String]
}

enum SortOrder {
ASC
DESC
}

enum SortableField {
title
timestamp
}

input Sort {
field:SortableField,
order:SortOrder = DESC
}

type AllPostResponse {
success: Boolean!
message: String
token: String
data: [Post]
}

Query:

getAllPost(sort: Sort,first: Int, offset: Int): AllPostResponse

Apollo server version:

“apollo-server”: “^2.19.2”,
“apollo-server-express”: “^2.21.0”,

Dgraph Version:

Using form the latest cloud: Dgraph Cloud

Getting below mentioned error:

Error:

/home/yuvaraj/node_modules/graphql/language/lexer.js:360
    throw (0, _syntaxError.syntaxError)(source, pos, unexpectedCharacterMessage(code));
    ^

GraphQLError [Object]: Syntax Error: Cannot parse the unexpected character "\u00A0".
    at syntaxError (/home/yuvaraj/node_modules/graphql/error/syntaxError.js:15:10)
    at readToken (/home/yuvaraj/node_modules/graphql/language/lexer.js:360:40)
    at Lexer.lookahead (/home/yuvaraj/node_modules/graphql/language/lexer.js:75:108)
    at Lexer.advance (/home/yuvaraj/node_modules/graphql/language/lexer.js:58:35)
    at Parser.expectOptionalToken (/home/yuvaraj/node_modules/graphql/language/parser.js:1425:19)
    at Parser.optionalMany (/home/yuvaraj/node_modules/graphql/language/parser.js:1499:14)
    at Parser.parseInputFieldsDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:1065:17)
    at Parser.parseInputObjectTypeDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:1049:23)
    at Parser.parseTypeSystemDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:720:23)
    at Parser.parseDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:150:23) {
  locations: [ { line: 121, column: 1 } ]
}
```Getting below mentioned error:

Error:

/home/yuvaraj/node_modules/graphql/language/lexer.js:360
    throw (0, _syntaxError.syntaxError)(source, pos, unexpectedCharacterMessage(code));
    ^

GraphQLError [Object]: Syntax Error: Cannot parse the unexpected character "\u00A0".
    at syntaxError (/home/yuvaraj/node_modules/graphql/error/syntaxError.js:15:10)
    at readToken (/home/yuvaraj/node_modules/graphql/language/lexer.js:360:40)
    at Lexer.lookahead (/home/yuvaraj/node_modules/graphql/language/lexer.js:75:108)
    at Lexer.advance (/home/yuvaraj/node_modules/graphql/language/lexer.js:58:35)
    at Parser.expectOptionalToken (/home/yuvaraj/node_modules/graphql/language/parser.js:1425:19)
    at Parser.optionalMany (/home/yuvaraj/node_modules/graphql/language/parser.js:1499:14)
    at Parser.parseInputFieldsDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:1065:17)
    at Parser.parseInputObjectTypeDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:1049:23)
    at Parser.parseTypeSystemDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:720:23)
    at Parser.parseDefinition (/home/yuvaraj/node_modules/graphql/language/parser.js:150:23) {
  locations: [ { line: 121, column: 1 } ]
}

When I use the schema you have provided, I don’t see any query called getAllPost.
image
Can you please review your query?
If you are trying some example from Dgraph documentation, can you please share the link where we can check what exactly you are trying? Thanks.

Use the query called “queryPost”

Please provide the exact query you are having an issue with. An exact query would like the one below.

query MyQuery {
  queryPost( first: 1, offset: 0){
    body
  }
}

Yes. This is working. How do I implement ordering in this same query using Apollo server

Please provide a description of what exactly you have configured on Apollo Server and what part did not work.

Schema:

type Post {
id: ID!
title: String!
timestamp: String!
isPrivate: Boolean!
avatar: String
body: String!
first: Int
offset: Int
}

Slash graphQL schema:

type Post {
id: ID!
title: String! @search(by: [fulltext])
timestamp: DateTime! @search
username: UserName @hasInverse(field: posts)
isPrivate: Boolean!
avatar: String
body: String
first: Int
offset: Int
}
enum SortOrder {
ASC
DESC
}

enum SortableField {
title
timestamp
}

input Sort {
field:SortableField,
order:SortOrder = DESC
}

type AllPostResponse {
success: Boolean!
message: String
token: String
data: [Post]
}

Query:

getAllPost(sort: Sort,first: Int, offset: Int): AllPostResponse

Apollo server version:

“apollo-server”: “^2.19.2”,
“apollo-server-express”: “^2.21.0”,

Problem facing:

“Unable to pass order in the args”.

“I want to sort the data based on timestamp”.

Handler function:

getAllPost: async (_parent, args, {req}) => {

let token = req.headers.authorization;

try {

  if(!token) throw new Error('Please provide the token');

  //auth midddleware
  let decodedToken = await authCheck(req).catch((e) => {
    throw new Error(e);
  });

  let currentUsername = await getCurrentUsernameService('queryUserName', decodedToken).catch((e) => {
    throw new Error(e);
  });

  let allPost = await getAllPostService('queryPost', args).catch((e) => {
    throw new Error(e);
  });

  return {
    success: true,
    message: 'Post is retrieved successfully',
    token,
    data: allPost
  };
} catch(e) {
  return {
    success: false,
    message: e[0] ? e[0].message : e.message,
    token,
    data: null
  };
}

},

DB Call:

const getAllPostService = async(name, args) => {
return new Promise(async(resolve, reject) => {
slashRetrieveQuery({
name,
args,
fields: id title timestamp isPrivate isReplyEnabled,
post
}).then(allPost => {
resolve(allPost);
}).catch((e) => {
reject(e[0].message);
});
});
};

Helpers:

const argsToString = (args) => {
if (typeof args === ‘object’) {

let argStrings = [];

Object.keys(args).forEach((key) => {

  if (typeof args[key] !== "boolean" && typeof args[key] !== "number" ) {
    argStrings.push(`${key}:"${args[key]}"`);
  } else {
    argStrings.push(`${key}:${args[key]}`);
  }
});

if (argStrings.length) {
  return `${argStrings.join(', ')}`;
}

}
return ‘’;
};

const slashRetrieveQuery = async ({ name, args, fields }) => {
return new Promise(async(resolve, reject) => {

let body = `
  query {
    ${name} (${argsToString(args)}) {
      ${fields}
    }
  }
`;

const fetchResult = await fetch(process.env.SLASH_ENDPOINT, {
  method,
  headers,
  body,
});

const result = await fetchResult.json();

if(result.hasOwnProperty('errors')) {
  return reject(result.errors);
}

return resolve(result.data[name]);

});
};

Postman request:

query {
getAllPost (sort:{field: timestamp} , first: 0, offset:0) {
success
message
token
data {
id
title
timestamp
isPrivate
avatar
body
}
}
}

This is a non-breaking space character. It appears this error is coming from your Apollo server Node/Express GraphQL schema and nothing to do with any problems related to Dgraph. I am moving this over to App Development.

Try fixing the original error in the schema by removing those bad characters and then update this on any remaining issues.


I am curious as to why you are needing to use Apollo Server/Express in your stack. What is your reason for wrapping the graphql endpoint provided by Dgraph in another layer?


Also it may help to clarify when asked for schema what schema is your Dgraph GraphQL schema, and what schema is your middle layer Apollo-Server/Express schema.