Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.

Commit b4e8448

Browse files
authored
Merge branch 'master' into feature/nonnull-list-elements
2 parents f1f8fe3 + 79e989e commit b4e8448

11 files changed

+65
-40
lines changed

demo/demo-data.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
{
8383
"id": "author",
8484
"type": "Array<Link<Entry>>",
85-
"linkedCt": "1kUEViTN4EmGiEaaeC6ouY"
85+
"linkedCts": ["1kUEViTN4EmGiEaaeC6ouY"]
8686
},
8787
{
8888
"id": "body",
@@ -91,7 +91,7 @@
9191
{
9292
"id": "category",
9393
"type": "Array<Link<Entry>>",
94-
"linkedCt": "5KMiN6YPvi42icqAUQMCQe"
94+
"linkedCts": ["5KMiN6YPvi42icqAUQMCQe"]
9595
},
9696
{
9797
"id": "tags",

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
]
2424
},
2525
"dependencies": {
26-
"cf-graphql": "^0.4.3",
26+
"cf-graphql": "^0.5.0",
2727
"cors": "^2.8.3",
2828
"express": "^4.15.3",
2929
"express-graphql": "^0.6.6",

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
},
5252
"dependencies": {
5353
"dataloader": "^1.3.0",
54-
"graphql": "^0.11.7",
54+
"https-proxy-agent": "^2.1.1",
5555
"lodash.camelcase": "^4.3.0",
5656
"lodash.chunk": "^4.2.0",
5757
"lodash.get": "^4.4.2",
@@ -65,6 +65,7 @@
6565
"express": "^4.15.4",
6666
"express-graphql": "^0.6.7",
6767
"fetch-graphql-schema": "^0.2.1",
68+
"graphql": "^0.13.0",
6869
"graphqlviz": "^2.0.1",
6970
"istanbul": "^0.4.5",
7071
"just-extend": "1.1.22",
@@ -73,5 +74,8 @@
7374
"proxyquire": "^1.8.0",
7475
"sinon": "^4.0.1",
7576
"tape": "^4.8.0"
77+
},
78+
"peerDependencies": {
79+
"graphql": "^0.11.7 || ^0.13.0"
7680
}
7781
}

src/backref-types.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
'use strict';
22

33
const _get = require('lodash.get');
4-
const {
5-
GraphQLNonNull,
6-
GraphQLObjectType,
7-
GraphQLList
8-
} = require('graphql');
4+
const {GraphQLNonNull, GraphQLString, GraphQLInt, GraphQLObjectType, GraphQLList} = require('graphql');
95

106
module.exports = createBackrefsType;
117

@@ -29,9 +25,20 @@ function prepareBackrefsFields (ct, ctIdToType) {
2925
function createBackrefFieldConfig (backref, Type) {
3026
return {
3127
type: new GraphQLList(new GraphQLNonNull(Type)),
32-
resolve: (entryId, _, ctx) => {
33-
return ctx.entryLoader.queryAll(backref.ctId)
34-
.then(entries => filterEntries(entries, backref.fieldId, entryId));
28+
args: {
29+
q: {type: GraphQLString},
30+
skip: {type: GraphQLInt},
31+
limit: {type: GraphQLInt},
32+
},
33+
resolve: (entryId, args, ctx) => {
34+
let q = `fields.${backref.fieldId}.sys.id[in]=${entryId}`;
35+
if (args.q) q = q + `&${args.q}`;
36+
37+
return ctx.entryLoader.query(backref.ctId, {
38+
q,
39+
skip: args.skip,
40+
limit: args.limit,
41+
})
3542
}
3643
};
3744
}

src/field-config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function createEntryFieldConfig (field, ctIdToType) {
7676
return createFieldConfig(typeFor(field, ctIdToType), field, (link, ctx) => {
7777
const linkedId = getLinkedId(link);
7878
if (isString(linkedId)) {
79-
return ctx.entryLoader.get(linkedId, field.linkedCt);
79+
return ctx.entryLoader.get(linkedId, field.linkedCts && field.linkedCts[0]);
8080
}
8181
});
8282
}
@@ -96,9 +96,9 @@ function getLinkedId (link) {
9696
return _get(link, ['sys', 'id']);
9797
}
9898

99-
function typeFor ({linkedCt}, ctIdToType = {}) {
100-
if (linkedCt) {
101-
return ctIdToType[linkedCt] || EntryType;
99+
function typeFor ({linkedCts}, ctIdToType = {}) {
100+
if (linkedCts && linkedCts[0]) {
101+
return ctIdToType[linkedCts[0]] || EntryType;
102102
} else {
103103
return EntryType;
104104
}

src/http-client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const os = require('os');
44
const qs = require('querystring');
55
const fetch = require('node-fetch');
66
const _get = require('lodash.get');
7+
const HttpProxyAgent = require('https-proxy-agent')
78

89
module.exports = createClient;
910

@@ -39,9 +40,13 @@ function get (url, params, opts) {
3940
const httpCall = {url, start: Date.now()};
4041
timeline.push(httpCall);
4142

43+
const agent = process.env.http_proxy
44+
? new HttpProxyAgent(process.env.http_proxy)
45+
: null
46+
4247
cache[url] = fetch(
4348
base + url,
44-
{headers: Object.assign({}, getUserAgent(), headers)}
49+
{headers: Object.assign({}, getUserAgent(), headers), agent}
4550
)
4651
.then(checkStatus)
4752
.then(res => {

src/prepare-space-graph.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function field (f) {
8282
return {
8383
id: f.id,
8484
type: type(f),
85-
linkedCt: linkedCt(f)
85+
linkedCts: linkedCts(f)
8686
};
8787
}
8888

@@ -117,15 +117,15 @@ function isEntityType (x) {
117117
return ENTITY_TYPES.indexOf(x) > -1;
118118
}
119119

120-
function linkedCt (f) {
120+
function linkedCts (f) {
121121
const prop = 'linkContentType';
122122
const validation = getValidations(f).find(v => {
123-
return Array.isArray(v[prop]) && v[prop].length === 1;
123+
return Array.isArray(v[prop]);
124124
});
125-
const linkedCt = validation && validation[prop][0];
125+
const linkedCts = validation && validation[prop];
126126

127-
if (linkedCt) {
128-
return linkedCt;
127+
if (linkedCts) {
128+
return linkedCts;
129129
}
130130
}
131131

@@ -144,15 +144,16 @@ function addBackrefs (spaceGraph) {
144144
}, {});
145145

146146
spaceGraph.forEach(ct => ct.fields.forEach(field => {
147-
if (field.linkedCt && byId[field.linkedCt]) {
148-
const linked = byId[field.linkedCt];
147+
if (field.linkedCts) field.linkedCts.forEach(linkedCt => {
148+
const linked = byId[linkedCt];
149+
if (!linked) return;
149150
linked.backrefs = linked.backrefs || [];
150151
linked.backrefs.push({
151152
ctId: ct.id,
152153
fieldId: field.id,
153-
backrefFieldName: `${ct.names.collectionField}__via__${field.id}`
154+
backrefFieldName: `${ct.names.collectionField}__via__${field.id}`,
154155
});
155-
}
156+
});
156157
}));
157158

158159
return spaceGraph;

test/backref-types.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,23 @@ test('backref-types: creating backrefers type', function (t) {
8383
}
8484
];
8585

86-
const ctx = {entryLoader: {queryAll: () => Promise.resolve(posts)}};
86+
const ctx = {entryLoader: {query: (ctId, {q}) => {
87+
const field = q.match(/^fields\.([^\.]*)\.sys\.id/)[1]
88+
const id = q.match(/=(.*)$/)[1]
89+
return Promise.resolve(posts.filter(post => {
90+
const f = post.fields[field]
91+
return f != null && (Array.isArray(f) ? f : [f]).find(item => item.sys.id === id)
92+
}));
93+
}}};
8794

8895
graphql(
8996
schema,
9097
'{ test { posts__via__category { title } posts__via__category2 { title } } }',
9198
null,
9299
ctx
93100
).then(res => {
101+
t.equal(res.errors, undefined);
94102
t.deepEqual(res.data.test.posts__via__category, [{title: 'p1t'}]);
95103
t.deepEqual(res.data.test.posts__via__category2, [{title: 'p3t'}]);
96-
t.equal(res.errors, undefined);
97104
});
98105
});

test/field-config.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ test('field-config: type for linked entry', function (t) {
127127
const types = {ct1: {}, ct2: {}};
128128
const tests = [
129129
[{}, undefined, EntryType],
130-
[{linkedCt: 'ct1'}, undefined, EntryType],
131-
[{linkedCt: 'ct2'}, {ct1: types.ct1, ct2: types.ct2}, types.ct2],
132-
[{linkedCt: 'ct3'}, {ct1: types.ct1, ct2: types.ct2}, EntryType]
130+
[{linkedCts: ['ct1']}, undefined, EntryType],
131+
[{linkedCts: ['ct2']}, {ct1: types.ct1, ct2: types.ct2}, types.ct2],
132+
[{linkedCts: ['ct3']}, {ct1: types.ct1, ct2: types.ct2}, EntryType]
133133
];
134134

135-
tests.forEach(([field, ctIdToType, Type]) => {
135+
tests.forEach(([field, ctIdToType, Type], i) => {
136136
const config = map['Link<Entry>'](field, ctIdToType);
137137
t.equal(config.type, Type);
138138
});

test/prepare-space-graph.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const test = require('tape');
44

5+
const flatmap = require('lodash/flatmap');
56
const prepareSpaceGraph = require('../src/prepare-space-graph.js');
67

78
const testCt = fields => ({sys: {id: 'ctid'}, name: 'test', fields});
@@ -155,9 +156,9 @@ test('prepare-space-graph: finding linked content types', function (t) {
155156
}, []);
156157

157158
const [p] = prepareSpaceGraph([testCt(fields)]);
158-
const linkedCts = p.fields.map(f => f.linkedCt).filter(id => typeof id === 'string');
159+
const linkedCts = flatmap(p.fields, f => f.linkedCts).filter(id => typeof id === 'string');
159160

160-
t.deepEqual(linkedCts, ['baz', 'baz']);
161+
t.deepEqual(linkedCts, ['foo', 'bar', 'foo', 'bar', 'baz', 'baz']);
161162

162163
t.end();
163164
});
@@ -167,7 +168,7 @@ test('prepare-space-graph: mixing field and items validations', function (t) {
167168
const fields = [{id: 'fid', type: 'Array', validations: [], items}];
168169
const [p] = prepareSpaceGraph([testCt(fields)]);
169170

170-
t.equal(p.fields[0].linkedCt, 'ctid');
171+
t.equal(p.fields[0].linkedCts[0], 'ctid');
171172

172173
t.end();
173174
});

0 commit comments

Comments
 (0)