Skip to content
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
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,22 @@ jobs:
- yarn test
- cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js

- stage: "Additional Tests"
name: "Floating Dependencies"
install:
- yarn install --no-lockfile --non-interactive
script:
- yarn test

# we recommend new addons test the current and previous LTS
# as well as latest stable release (bonus points to beta/canary)
- stage: "Additional Tests"
env: EMBER_TRY_SCENARIO=ember-lts-2.16
- env: EMBER_TRY_SCENARIO=ember-lts-2.18
- env: EMBER_TRY_SCENARIO=ember-lts-3.4
- env: EMBER_TRY_SCENARIO=ember-lts-3.8
- env: EMBER_TRY_SCENARIO=ember-lts-3.12
- env: EMBER_TRY_SCENARIO=ember-lts-3.16
- env: EMBER_TRY_SCENARIO=ember-release
- env: EMBER_TRY_SCENARIO=ember-beta
- env: EMBER_TRY_SCENARIO=ember-canary
- env: EMBER_TRY_SCENARIO=ember-default-with-jquery
- env: EMBER_TRY_SCENARIO=ember-classic

script:
- node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO
10 changes: 8 additions & 2 deletions addon/fields/selections/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { GraphQLUnionType } from 'graphql';
import createFieldInfo from '../info/create';
import { getFieldNameAndAlias } from '../name';
import { fragmentFieldsReducer, inlineFragmentFieldsReducer } from './fragments';
import { partial, unwrapNonNull } from '../../utils';
import { getUnionField, partial, unwrapNonNull } from '../../utils';

const getFieldType = (type, fieldName) => {
if (fieldName === '__typename') {
Expand All @@ -10,7 +11,12 @@ const getFieldType = (type, fieldName) => {

type = unwrapNonNull(type);

let field = type._fields[fieldName];
let field;
if (type instanceof GraphQLUnionType) {
field = getUnionField(type, fieldName);
} else {
field = type._fields[fieldName];
}
let fieldType = unwrapNonNull(field.type);

return fieldType;
Expand Down
8 changes: 8 additions & 0 deletions addon/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export const ensureList = (item) => item == null

export const getFirstKey = (obj) => Object.keys(obj)[0];

export const getUnionField = (type, fieldName) => {
for (const t of type._types) {
if (t._fields[fieldName]) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be an edge case, but would there be an issue if multiple types had the same fieldName but the fields were different types?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be an issue but addressing it seems like a much larger piece of work that can potentially come later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this would be an issue. I feel the appropriate thing would be to add full UnionType functionality at that point, where this is a simple fix that allows us to add UnionTypes and use mapping to accomplish it

return t._fields[fieldName];
}
}
}

export const isFunction = (obj) => obj != null && typeof obj === 'function';

export const objectOfType = (obj, typeName) =>
Expand Down
59 changes: 21 additions & 38 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,18 @@ module.exports = async function() {
useYarn: true,
scenarios: [
{
name: 'ember-lts-2.16',
env: {
EMBER_OPTIONAL_FEATURES: JSON.stringify({ 'jquery-integration': true })
},
npm: {
devDependencies: {
'@ember/jquery': '^0.5.1',
'ember-apollo-client': '2.0.0-beta.1',
'ember-source': '~2.16.0'
}
}
},
{
name: 'ember-lts-2.18',
env: {
EMBER_OPTIONAL_FEATURES: JSON.stringify({ 'jquery-integration': true })
},
npm: {
devDependencies: {
'@ember/jquery': '^0.5.1',
'ember-apollo-client': '2.0.0-beta.1',
'ember-source': '~2.18.0'
}
}
},
{
name: 'ember-lts-3.4',
name: 'ember-lts-3.12',
npm: {
devDependencies: {
'ember-apollo-client': '2.0.0-beta.1',
'ember-source': '~3.4.0'
'ember-source': '~3.12.0'
}
}
},
{
name: 'ember-lts-3.8',
name: 'ember-lts-3.16',
npm: {
devDependencies: {
'ember-source': '~3.8.0'
'ember-source': '~3.16.0'
}
}
},
Expand Down Expand Up @@ -73,31 +46,41 @@ module.exports = async function() {
}
}
},
// The default `.travis.yml` runs this scenario via `yarn test`,
// not via `ember try`. It's still included here so that running
// `ember try:each` manually or from a customized CI config will run it
// along with all the other scenarios.
{
name: 'ember-default',
npm: {
devDependencies: {}
}
},
{
name: 'ember-default-with-jquery',
env: {
EMBER_OPTIONAL_FEATURES: JSON.stringify({
'jquery-integration': true
})
},
npm: {
devDependencies: {
'@ember/jquery': '^0.5.1',
'ember-apollo-client': '2.0.0-beta.1'
'@ember/jquery': '^0.5.1'
}
}
},
{
name: 'ember-default-with-jquery',
name: 'ember-classic',
env: {
EMBER_OPTIONAL_FEATURES: JSON.stringify({
'jquery-integration': true
'application-template-wrapper': true,
'default-async-observers': false,
'template-only-glimmer-components': false
})
},
npm: {
devDependencies: {
'@ember/jquery': '^0.5.1',
'ember-apollo-client': '2.0.0-beta.1'
ember: {
edition: 'classic'
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"broccoli-asset-rev": "^3.0.0",
"clean-css": "^4.1.11",
"coveralls": "^3.0.2",
"ember-apollo-client": "^2.0.0-beta.7",
"ember-apollo-client": "^2.0.0",
"ember-cli": "~3.13.1",
"ember-cli-code-coverage": "^1.0.0-beta.8",
"ember-cli-dependency-checker": "^3.1.0",
Expand All @@ -49,6 +49,7 @@
"ember-fetch": "^6.4.0",
"ember-load-initializers": "^2.1.0",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-native-class-polyfill": "^1.0.6",
"ember-qunit": "^4.5.1",
"ember-resolver": "^5.3.0",
"ember-source": "~3.14.1",
Expand Down
40 changes: 40 additions & 0 deletions tests/acceptance/union-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit } from '@ember/test-helpers';

module('Acceptance | union', function(hooks) {
setupApplicationTest(hooks);

test('it is able to resolve union types', async function(assert) {
let person = this.server.create('person', {
firstName: 'Alice',
lastName: 'Example',
age: 30
});
let pet = this.server.create('animal', {
name: 'Beanie',
age: 10
});

await visit('/pets-and-people');

assert
.dom('.pet-id')
.containsText(pet.id);
assert
.dom('.pet-name')
.containsText(pet.name);
assert
.dom('.pet-age')
.containsText(pet.age);
assert
.dom('.person-id')
.containsText(person.id);
assert
.dom('.person-name')
.containsText(`${person.firstName} ${person.lastName}`);
assert
.dom('.person-age')
.containsText(person.age);
})
});
15 changes: 15 additions & 0 deletions tests/dummy/app/gql/queries/pets-and-people.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query {
petsAndPeople {
... on Pet {
id
age
name
}
... on Person {
id
age
firstName
lastName
}
}
}
4 changes: 4 additions & 0 deletions tests/dummy/app/gql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ input PersonAttributes {
age: Int
}

union PetOrPerson = Pet | Person

type Query {
node(id: ID!): Node

Expand All @@ -104,4 +106,6 @@ type Query {
peopleSameAgeAsDogYears: [Person]

nonNullListOfPeople: [Person!]!

petsAndPeople: [PetOrPerson!]!
}
3 changes: 3 additions & 0 deletions tests/dummy/app/helpers/eq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { helper } from '@ember/component/helper';

export default helper(([a, b]) => a === b);
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Router.map(function() {
this.route('person', { path: '/person/:person_id' }, function() {
this.route('edit');
});
this.route('pets-and-people');
});

export default Router;
12 changes: 12 additions & 0 deletions tests/dummy/app/routes/pets-and-people.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Route from '@ember/routing/route';
import query from 'dummy/gql/queries/pets-and-people';
import { queryManager } from 'ember-apollo-client';

export default Route.extend({
apollo: queryManager(),

model() {
const apollo = this.get('apollo');
return apollo.watchQuery({ query });
}
});
26 changes: 26 additions & 0 deletions tests/dummy/app/services/apollo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ApolloService from 'ember-apollo-client/services/apollo';
Comment thread
jneurock marked this conversation as resolved.
import {
IntrospectionFragmentMatcher,
InMemoryCache
} from 'apollo-cache-inmemory';
import schema from 'dummy/gql/schema';
const unionTypes = schema.definitions.filter(d => d.kind === 'UnionTypeDefinition');
const introspectionQueryResultData = {
__schema: {
types: unionTypes.map(t => ({
kind: 'UNION',
name: t.name.value,
possibleTypes: t.types.map(t => ({ name: t.name.value }))
}))
}
};

export default class extends ApolloService {
cache() {
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData
});

return new InMemoryCache({ fragmentMatcher });
}
}
11 changes: 11 additions & 0 deletions tests/dummy/app/templates/pets-and-people.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#each model.petsAndPeople as |p|}}
{{#if (eq p.__typename "Pet")}}
<div class="pet-id">{{p.id}}</div>
<div class="pet-name">{{p.name}}</div>
<div class="pet-age">{{p.age}}</div>
{{else}}
<div class="person-id">{{p.id}}</div>
<div class="person-name">{{p.firstName}} {{p.lastName}}</div>
<div class="person-age">{{p.age}}</div>
{{/if}}
{{/each}}
6 changes: 6 additions & 0 deletions tests/dummy/mirage/handlers/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ const OPTIONS = {
type === 'dog' && age * 7 === person.age).length);

return records;
},
petsAndPeople(_, db) {
let pets = db.animals.map(a => ({ ...a, __typename: 'Pet' }));
let people = db.people.map(p => ({ ...p, __typename: 'Person' }));

return [...pets, ...people];
}
},
mutations: {
Expand Down
Loading