Skip to content

Commit

Permalink
Delete the escaped field name in favor of unescaped/original field na…
Browse files Browse the repository at this point in the history
…me to keep the expected structure (#4220)
  • Loading branch information
ardatan authored Aug 3, 2022
1 parent 5becf93 commit 961e071
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .changeset/silver-monkeys-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
"@omnigraph/json-schema": patch
---

Delete the escaped field name in favor of unescaped/original field name to keep the expected structure

For example if you have `foo-bar` in your request input, it is sanitized to `foo_bar` in order to fit the requirements of GraphQL specification. But then, JSON Schema loader recovers it to the original `foo-bar` field name before sending request. But it was sending both field names.

```graphql
input FooBar {
foo_bar: String
}
```

Before;
```json
{
"foo-bar": "baz",
"foo_bar": "baz"
}
```

After;
```json
{
"foo-bar": "baz"
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export function resolveDataByUnionInputType(data: any, type: GraphQLInputType, s
const resolvedData = resolveDataByUnionInputType(data[fieldName], field.type, schemaComposer);
return resolvedData;
}
const fieldData = data[fieldName];
data[fieldName] = undefined;
const realFieldName = (field.extensions?.propertyName as string) || fieldName;
data[realFieldName] = resolveDataByUnionInputType(data[fieldName], field.type, schemaComposer);
data[realFieldName] = resolveDataByUnionInputType(fieldData, field.type, schemaComposer);
}
}
}
Expand Down
107 changes: 107 additions & 0 deletions packages/loaders/json-schema/test/execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,111 @@ describe('Execution', () => {
});
});
});
describe('Sanization', () => {
it('should recover escaped input field names before preparing the request for GET', async () => {
const schema = await loadGraphQLSchemaFromJSONSchemas('test', {
async fetch(info: RequestInfo, init?: RequestInit) {
let request: Request;
if (typeof info !== 'object') {
request = new Request(info, init);
} else {
request = info;
}
return new Response(
JSON.stringify({
url: request.url,
}),
{
headers: {
'Content-Type': 'application/json',
},
}
);
},
baseUrl: 'http://localhost:3000',
operations: [
{
type: OperationTypeNode.QUERY,
field: 'test',
method: 'GET',
path: '/test',
requestSample: {
'foo-bar': 'baz',
},
responseSample: {
url: 'http://localhost:3000/test?foo-bar=baz',
},
},
],
});

const query = /* GraphQL */ `
query Test {
test(input: { foo_bar: "baz" }) {
url
}
}
`;

const result = await execute({
schema,
document: parse(query),
});

expect(result).toEqual({
data: {
test: {
url: `http://localhost:3000/test?foo-bar=baz`,
},
},
});
});
it('should recover escaped input field names before preparing the request for POST', async () => {
const expectedInput = {
'foo-bar': 'baz',
};
let receivedInput: typeof expectedInput;
const schema = await loadGraphQLSchemaFromJSONSchemas('test', {
async fetch(info: RequestInfo, init?: RequestInit) {
let request: Request;
if (typeof info !== 'object') {
request = new Request(info, init);
} else {
request = info;
}
receivedInput = await request.json();
return new Response(JSON.stringify({}), {
headers: {
'Content-Type': 'application/json',
},
});
},
baseUrl: 'http://localhost:3000',
operations: [
{
type: OperationTypeNode.QUERY,
field: 'test',
method: 'POST',
path: '/test',
requestSample: expectedInput,
},
],
});

const query = /* GraphQL */ `
query Test {
test(input: { foo_bar: "baz" }) {
foo_bar
}
}
`;

await execute({
schema,
document: parse(query),
});

expect(receivedInput).toEqual(expectedInput);
});
});
});

0 comments on commit 961e071

Please sign in to comment.