Skip to content

add support for Apollo @rest directive #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/__tests__/directives.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,70 @@ describe('jsonToGraphQLQuery() - directives', () => {
}`);
});

it('places args in front of drirective if it is @rest', () => {
const path = "/foo"

const query = {
query: {
Posts: {
__args: {
where: {
id: 10,
},
orderBy: 'flibble'
},
__directives: {
rest: { path }
},
id: true,
title: true,
post_date: true
}
}
} as any;
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
`query {
Posts (where: {id: 10}, orderBy: "flibble") @rest(path: "/foo") {
id
title
post_date
}
}`);
});

it('returns strings, arrays and objects in the directive args', () => {
const string = "/foo"
const object = { bar: 'baz' }
const array = ["complexities", "of", "naming", "conventions"]

const query = {
query: {
Posts: {
__args: {
where: {
id: 10,
},
orderBy: 'flibble'
},
__directives: {
client: { string, object, array }
},
id: true,
title: true,
post_date: true
}
}
} as any;
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
`query {
Posts @client(string: "/foo", object: {bar: "baz"}, array: ["complexities", "of", "naming", "conventions"]) (where: {id: 10}, orderBy: "flibble") {
id
title
post_date
}
}`);
});

it('converts a complex query with directives with no arguments', () => {
const query = {
query: {
Expand Down
9 changes: 7 additions & 2 deletions src/jsonToGraphQLQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function buildDirectives(dirsObj: any): string {
else if (typeof directiveValue === 'object') {
const args = [];
for (const argName in directiveValue) {
const argVal = stringify(directiveValue[argName]).replace(/"/g, '');
const argVal = stringify(directiveValue[argName]);
args.push(`${argName}: ${argVal}`);
}
return `${directiveName}(${args.join(', ')})`;
Expand Down Expand Up @@ -109,6 +109,7 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
else if (argsExist || directivesExist) {
let argsStr: string;
let dirsStr: string;
let dirStrIsRest: boolean = false;
if (directivesExist) {
// TODO: Add support for multiple directives on one node.
const numDirectives = Object.keys(value.__directives).length;
Expand All @@ -122,8 +123,12 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
if (argsExist) {
argsStr = `(${buildArgs(value.__args)})`;
}
if (dirsStr && dirsStr.includes('@rest')) {
dirStrIsRest = true
}
const spacer = directivesExist && argsExist ? ' ' : '';
token = `${token} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`;

token = `${token}${dirStrIsRest && argsStr ? ` ${argsStr} ` : ' '}${dirsStr ? dirsStr : ''}${spacer}${!dirStrIsRest && argsStr ? argsStr : ''}`;
}

// DEPRECATED: Should be removed in version 2.0.0
Expand Down