Skip to content
Closed
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
2 changes: 1 addition & 1 deletion examples/hello-world/client/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h2>Add new</h2>
<h2>List</h2>

<ul>
<li *ngFor="let user of users | async | apolloQuery:'users'">
<li *ngFor="let user of users | async | apolloQuery">
{{ user.firstName }} {{ user.lastName }}
</li>
</ul>
9 changes: 5 additions & 4 deletions examples/hello-world/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import ApolloClient, {
import gql from 'apollo-client/gql';

import {
graphQLResult
graphQLResult,
} from 'graphql';

const client = new ApolloClient({
Expand Down Expand Up @@ -89,9 +89,10 @@ const client = new ApolloClient({
},
})
class Main {
users: Observable<any[]>;
firstName: string;
lastName: string;
public addUser: Function;
public users: Observable<any[]>;
public firstName: string;
public lastName: string;

public newUser() {
this.addUser(this.firstName)
Expand Down
19 changes: 18 additions & 1 deletion src/apolloQueryPipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@ import {
Pipe,
} from '@angular/core';

import {
values,
isEmpty,
isObject,
} from 'lodash';

@Pipe({
name: 'apolloQuery',
})
export class ApolloQueryPipe {
public transform(obj: any, name: string = '') {
if (obj && obj.data && name !== '' && obj.data[name]) {
if (!isObject(obj)) {
return;
}

// an empty object by default
obj = (obj || {});

if (isEmpty(name)) {
return values(obj.data)[0];
}

if (obj.data) {
return obj.data[name];
}
}
Expand Down
33 changes: 27 additions & 6 deletions tests/apolloQueryPipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,35 @@ describe('ApolloQueryPipe', () => {
pipe = new ApolloQueryPipe();
});

it('should capitalize all words in a string', () => {
const object = {
it('should return nothing if first argument is not an object', () => {
expect(pipe.transform('test')).toBeUndefined();
});

it('should return nothing on empty object', () => {
expect(pipe.transform({})).toBeUndefined();
});

it('should return nothing on empty data', () => {
expect(pipe.transform({
data: {},
})).toBeUndefined();
});

it('should use the first property of data if name is not defined', () => {
expect(pipe.transform({
data: {
foo: 'bar',
foo: 'foo',
bar: 'bar',
},
};
const result = pipe.transform(object, 'foo');
})).toEqual('foo');
});

expect(result).toEqual(object.data.foo);
it('should use a property where key matches name', () => {
expect(pipe.transform({
data: {
foo: 'foo',
bar: 'bar',
},
}, 'bar')).toEqual('bar');
});
});