Skip to content
This repository was archived by the owner on Aug 14, 2019. It is now read-only.

Commit e8d7ba0

Browse files
committed
fix some styles and lint problem
1 parent 39c8017 commit e8d7ba0

File tree

7 files changed

+29
-30
lines changed

7 files changed

+29
-30
lines changed

examples/simple-todo/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Example extends Component<{}, State> {
4545
Apollo-link-firebase Simple Todo Example
4646
</Header>
4747
<Content style={{padding: '0 50px'}}>
48-
<div style={{background: '#fff', padding: 24, minHeight: '100%'}}>
48+
<div style={{background: '#fff', padding: 24, minHeight: 280}}>
4949
{
5050
(this.state.user) ?
5151
<Row>

src/rtdb/link.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { OperationTypeNode } from 'graphql';
2-
import { graphql, ExecInfo } from 'graphql-anywhere/lib/async';
2+
import { graphql } from 'graphql-anywhere/lib/async';
33
import { ApolloLink, Observable, FetchResult, Operation, NextLink } from 'apollo-link';
4-
import { hasDirectives, addTypenameToDocument, getMainDefinition, getFragmentDefinitions } from 'apollo-utilities';
4+
import { hasDirectives, addTypenameToDocument, getMainDefinition } from 'apollo-utilities';
55
import { database as firebaseDatabase } from 'firebase';
66
import { Resolver } from 'graphql-anywhere';
77
import { ResolverContext } from './types';
8-
import { createQuery } from './utils';
98

109
// resolvers
1110
import queryResolver from './queryResolver';

src/rtdb/mutationResolver.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const resolver: Resolver = async (
1313
) => {
1414
const { resultKey, directives, isLeaf } = info;
1515
const { database } = context;
16+
const hasTypeDirective = has(directives, 'type');
1617

1718
// used when rtdbPush need to know the generated pushKey
1819
// also fields return in @rtdbPush payload
@@ -29,7 +30,7 @@ const resolver: Resolver = async (
2930

3031
// __typename
3132
if (isLeaf && resultKey === '__typename') {
32-
return has(directives, 'type')
33+
return hasTypeDirective
3334
? directives.type.name
3435
: root.__typename || null;
3536
}
@@ -43,9 +44,9 @@ const resolver: Resolver = async (
4344
// https://dev-blog.apollodata.com/designing-graphql-mutations-e09de826ed97
4445
const payload: any = args && args.input;
4546
// deal with @rtdbUpdate, @rtdbSet, @rtdbRemove
46-
const typeTagName: string = has(directives, 'type')
47+
const typeTagName: string = hasTypeDirective
4748
? directives.type.name
48-
: null
49+
: null;
4950
if (has(directives, 'rtdbUpdate')) {
5051
const {ref, type} = directives.rtdbUpdate;
5152
context.mutationRef = ref;
@@ -71,7 +72,8 @@ const resolver: Resolver = async (
7172
__pushKey: newRef.key,
7273
__typename: typeTagName || type
7374
};
74-
} else if (!isLeaf && root && has(root, 'payload') && has(directives, 'type')) {
75+
} else if (!isLeaf && root && has(root, 'payload') && hasTypeDirective) {
76+
// if it's a selectionSet with payload and @type
7577
return {
7678
payload: (root.payload && root.payload[resultKey]) || null,
7779
__typename: directives.type.name

src/rtdb/queryResolver.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const queryResolver: Resolver = async (
2727
const { directives, isLeaf, resultKey } = info;
2828
const { database, exportVal } = context;
2929
const {rootSnapshot} = root;
30+
const hasTypeDirective = has(directives, 'type');
3031
let currentSnapshot = root.__snapshot;
3132

3233
if (isLeaf) {
@@ -35,9 +36,9 @@ const queryResolver: Resolver = async (
3536

3637
// typename
3738
if (resultKey === '__typename') {
38-
return has(directives, 'type')
39-
? directives.type.name
40-
: root.__typename || null;
39+
return hasTypeDirective
40+
? directives.type.name
41+
: root.__typename || null;
4142
}
4243

4344
// dealing with different directives
@@ -81,28 +82,26 @@ const queryResolver: Resolver = async (
8182

8283
// if it's nested selectionSet, we return the child
8384
if (!isLeaf && !has(directives, 'rtdbQuery') && !rootSnapshot) {
85+
// tslint:disable-next-line:no-shadowed-variable
86+
const typename = hasTypeDirective ? directives.type.name : null;
8487
return (has(directives, 'array'))
85-
? snapshotToArray(currentSnapshot.child(resultKey), has(directives, 'type') ? directives.type.name : null)
86-
: has(directives, 'type')
87-
? {
88-
__snapshot: currentSnapshot.child(resultKey),
89-
__typename: directives.type.name
90-
}
91-
: {
92-
__snapshot: currentSnapshot.child(resultKey)
93-
};
88+
? snapshotToArray(currentSnapshot.child(resultKey), typename)
89+
: {
90+
__snapshot: currentSnapshot.child(resultKey),
91+
__typename: typename
92+
};
9493
}
9594

9695
// type could be defined in different directives, @rtdbQuery, @rtdbSub...
97-
const type = has(directives, 'type') ? directives.type.name : context.findType(directives);
96+
const typename = hasTypeDirective ? directives.type.name : context.findType(directives);
9897

9998
// firebase treat all data as object, even array
10099
// so we need a hint using @array to know when to parse object to array
101100
return (has(directives, 'array'))
102-
? snapshotToArray(currentSnapshot, type)
101+
? snapshotToArray(currentSnapshot, typename)
103102
: {
104103
__snapshot: currentSnapshot,
105-
__typename: type
104+
__typename: typename
106105
};
107106
};
108107

src/rtdb/subscriptionLink.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OperationTypeNode, FieldNode } from 'graphql';
1+
import { FieldNode } from 'graphql';
22
import { ApolloLink, Observable, FetchResult, Operation, NextLink } from 'apollo-link';
33
import {
44
hasDirectives, addTypenameToDocument, getMainDefinition, getFragmentDefinitions, getDirectiveInfoFromField
@@ -36,7 +36,7 @@ export default class RtdbSubLink extends ApolloLink {
3636
};
3737

3838
// Subscription operations must have exactly one root field.
39-
const onlyRootField: FieldNode = mainDefinition.selectionSet.selections[0];
39+
const onlyRootField: FieldNode = mainDefinition.selectionSet.selections[0] as FieldNode;
4040

4141
// get directives
4242
const directives = getDirectiveInfoFromField(onlyRootField, operation.variables);

src/rtdb/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { OperationTypeNode, OperationDefinitionNode, FragmentDefinitionNode } from 'graphql';
21
import { database } from 'firebase';
32

43
export interface ResolverContext {

test/rtdbLink.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,10 @@ describe('rtdbLink', () => {
517517
}
518518
}),
519519
);
520-
expect(data.articles[0]['__typename']).to.be.eql('Article');
521-
expect(data.articles[0]['nested']['__typename']).to.be.eql('NestedType');
522-
expect(data.articles[0].authors[0]['__typename']).to.be.eql('Author');
523-
expect(data.articles[0].authors[0]['info']['__typename']).to.be.eql('AuthorInfo');
520+
expect(data.articles[0].__typename).to.be.eql('Article');
521+
expect(data.articles[0].nested.__typename).to.be.eql('NestedType');
522+
expect(data.articles[0].authors[0].__typename).to.be.eql('Author');
523+
expect(data.articles[0].authors[0].info.__typename).to.be.eql('AuthorInfo');
524524
});
525525
});
526526

0 commit comments

Comments
 (0)