Skip to content

Commit

Permalink
We return schema with resources (#100)
Browse files Browse the repository at this point in the history
xref: app-sre/qontract-validator#25

Signed-off-by: Rafa Porres Molina <[email protected]>
  • Loading branch information
rporres authored Dec 22, 2020
1 parent 53f9e26 commit add8381
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,20 @@ const createSchemaType = (app: express.Express, bundleSha: string, conf: any) =>
);
} else if (fieldInfo.isResource) {
// resource
fieldDef['args'] = { path: { type: GraphQLString } };
fieldDef['resolve'] = (root: any, args: any) =>
args.path ?
[app.get('bundles')[bundleSha].resourcefiles.get(args.path)] :
Array.from(app.get('bundles')[bundleSha].resourcefiles.values());
fieldDef['args'] = { path: { type: GraphQLString }, schema: { type: GraphQLString } };
fieldDef['resolve'] = (root: any, args: any) => {
if (args.path) {
return [app.get('bundles')[bundleSha].resourcefiles.get(args.path)];
}

let results = Array.from(app.get('bundles')[bundleSha].resourcefiles.values());

if (args.schema) {
results = results.filter((r: any) => r.$schema === args.schema);
}

return results;
};
}

// return
Expand Down
4 changes: 4 additions & 0 deletions test/graphql_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@
"isRequired": true,
"type": "string",
"name": "sha256sum"
},
{
"type": "string",
"name": "schema"
}
],
"name": "Resource_v1"
Expand Down
6 changes: 5 additions & 1 deletion test/schemas/schemas.data.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@
"isRequired": true,
"type": "string",
"name": "sha256sum"
},
{
"type": "string",
"name": "schema"
}
],
"name": "Resource_v1"
Expand Down Expand Up @@ -1180,4 +1184,4 @@
}
],
"resources": {}
}
}
13 changes: 12 additions & 1 deletion test/server.data.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@
"isRequired": true,
"type": "string",
"name": "sha256sum"
},
{
"type": "string",
"name": "schema"
}
],
"name": "Resource_v1"
Expand Down Expand Up @@ -1236,7 +1240,14 @@
"/resource1.yml": {
"content": "test resource",
"path": "/resource1.yml",
"sha256sum": "ff"
"sha256sum": "ff",
"$schema": null
},
"/prometheus-resource.yml": {
"content": "prometheus test resource",
"path": "/prometheus-resource.yml",
"sha256sum": "ee",
"$schema": "/openshift/prometheus-rule-1.yml"
}
}
}
57 changes: 57 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,66 @@ describe('server', async () => {
.set('content-type', 'application/json')
.send({ query });
responseIsNotAnError(response);
response.body.data.resources.length.should.equal(1);
return response.body.data.resources[0].content.should.equal('test resource');
});

it('can retrieve a resource with a non-empty schema', async () => {
const query = `{
resources: resources_v1(path: "/prometheus-resource.yml") {
content
sha256sum
path
schema
}
}`;

const response = await chai.request(srv)
.post('/graphql')
.set('content-type', 'application/json')
.send({ query });
responseIsNotAnError(response);
response.body.data.resources.length.should.equal(1);
return response.body.data.resources[0].schema.should.equal('/openshift/prometheus-rule-1.yml');
});

it('can search a resource by schema', async () => {
const query = `{
resources: resources_v1(schema: "/openshift/prometheus-rule-1.yml") {
content
sha256sum
path
schema
}
}`;

const response = await chai.request(srv)
.post('/graphql')
.set('content-type', 'application/json')
.send({ query });
responseIsNotAnError(response);
response.body.data.resources.length.should.equal(1);
return response.body.data.resources[0].path.should.equal('/prometheus-resource.yml');
});

it('can retrieve all resources', async () => {
const query = `{
resources: resources_v1 {
content
sha256sum
path
schema
}
}`;

const response = await chai.request(srv)
.post('/graphql')
.set('content-type', 'application/json')
.send({ query });
responseIsNotAnError(response);
return response.body.data.resources.length.should.equal(2);
});

it('can search by path', async () => {
const query = `{
roles_v1(path: "/role-A.yml") {
Expand Down

0 comments on commit add8381

Please sign in to comment.