Skip to content
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

Updated interface support #121

Merged
merged 1 commit into from
Mar 6, 2019
Merged
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
251 changes: 251 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "discountApplication",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INTERFACE",
"name": "DiscountApplication",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand Down Expand Up @@ -455,6 +471,241 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "ENUM",
"name": "DiscountApplicationTargetSelection",
"description": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "ALL",
"description": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "ENTITLED",
"description": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "EXPLICIT",
"description": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{
"kind": "ENUM",
"name": "DiscountApplicationTargetType",
"description": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "LINE_ITEM",
"description": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "SHIPPING_LINE",
"description": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "DiscountCodeApplication",
"description": null,
"fields": [
{
"name": "code",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "targetSelection",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "DiscountApplicationTargetSelection",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "targetType",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "DiscountApplicationTargetType",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "DiscountApplication",
"ofType": null
}
],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INTERFACE",
"name": "DiscountApplication",
"fields": [
{
"name": "targetSelection",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "DiscountApplicationTargetSelection",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "targetType",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "DiscountApplicationTargetType",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": [
{
"kind": "OBJECT",
"name": "DiscountCodeApplication",
"ofType": null
},
{
"kind": "OBJECT",
"name": "ManualDiscountApplication",
"ofType": null
}
]
},
{
"kind": "OBJECT",
"name": "ManualDiscountApplication",
"description": null,
"fields": [
{
"name": "targetSelection",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "DiscountApplicationTargetSelection",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "targetType",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "DiscountApplicationTargetType",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "title",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "DiscountApplication",
"ofType": null
}
],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INTERFACE",
"name": "Node",
Expand Down
6 changes: 4 additions & 2 deletions src/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ function transformScalars(context, value) {
}

function recordTypeInformation(context, value) {
const {typeBundle, typeSchema} = context.selection.selectionSet;

if (isValue(value)) {
if (value.__typename) {
value.type = schemaForType(context.selection.selectionSet.typeBundle, value.__typename);
value.type = schemaForType(typeBundle, value.__typename, typeSchema);
} else {
value.type = context.selection.selectionSet.typeSchema;
value.type = typeSchema;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/schema-for-type.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export default function schemaForType(typeBundle, typeName) {
export default function schemaForType(typeBundle, typeName, typeSchema = null) {
const type = typeBundle.types[typeName];

if (type) {
return type;
} else if (typeSchema && typeSchema.kind === 'INTERFACE') {
return typeSchema;
}

throw new Error(`No type of ${typeName} found in schema`);
}

63 changes: 63 additions & 0 deletions test/decode-unknown-type-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import assert from 'assert';
import decode from '../src/decode';
import Query from '../src/query';
import typeBundle from '../fixtures/types'; // eslint-disable-line import/no-unresolved

const checkoutQuery = new Query(typeBundle, (root) => {
root.add('node', {args: {id: 'gid://shopify/Checkout/1'}}, (node) => {
node.addInlineFragmentOn('Checkout', (checkout) => {
checkout.add('discountApplication', (discount) => {
discount.add('targetSelection');
discount.add('targetType');
discount.addInlineFragmentOn('DiscountCodeApplication', (application) => {
application.add('code');
});
});
});
});
});

suite('decode-unknown-type-test', () => {
test('it decodes an interface into a type', () => {
const data = {
node: {
__typename: 'Checkout',
discountApplication: {
__typename: 'DiscountCodeApplication',
targetSelection: 'ALL',
targetType: 'SHIPPING_LINE',
code: 'SHIPPINGFREE'
}
}
};

const decoded = decode(checkoutQuery, data);

assert.ok(decoded);
assert.equal(decoded.node.discountApplication.type.name, 'DiscountCodeApplication');
assert.equal(decoded.node.discountApplication.targetSelection, data.node.discountApplication.targetSelection);
assert.equal(decoded.node.discountApplication.targetType, data.node.discountApplication.targetType);
assert.equal(decoded.node.discountApplication.code, data.node.discountApplication.code);
});

test('it decodes an unknown interface into a base type', () => {
const data = {
node: {
__typename: 'Checkout',
discountApplication: {
__typename: 'AutomaticDiscountApplication',
targetSelection: 'ALL',
targetType: 'SHIPPING_LINE'
}
}
};

const decoded = decode(checkoutQuery, data);

assert.ok(decoded);
assert.equal(decoded.node.discountApplication.type.name, 'DiscountApplication');
assert.equal(decoded.node.discountApplication.targetSelection, data.node.discountApplication.targetSelection);
assert.equal(decoded.node.discountApplication.targetType, data.node.discountApplication.targetType);
assert.equal(decoded.node.discountApplication.code, null);
});
});
Loading