
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
A way of using GraphQL (through Apollo Client) in your Polymer 2.0 elements through class mixins.
Kapton is also the name of a polymer used on the Apollo Lunar Module.
A sample application is available at https://github.com/atoy40/kapton-sample
This is the GraphQL schema used in the examples below in GraphQL schema language.
type User {
uid: String!
lastname: String!
}
type RootQuery {
users(limit: Int): [User!]
}
type RootMutation {
addUser(uid: String!, lastname: String!) : User
}
schema {
query: RootQuery
mutation: RootMutation
}
You'll probably use a javascript entry point "packaged" using a tool like webpack or browserify. The following example use webpack
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import Kapton, { createKaptonMixin } from 'kapton';
import gql from 'graphql-tag';
// Create the apollo client
const apolloClient = new ApolloClient({
networkInterface: createNetworkInterface({
uri: 'http://localhost:8080/graphql',
}),
// define unique id of User's
dataIdFromObject: (result) => {
return result.__typename+'_'+result.uid;
}
});
// get a mixin "factory"
const graphql = Kapton({apolloClient});
// get a query document
const USERS_LIST = gql`
query myQuery($limit: Int!) {
users(limit: $limit) {
uid
lastname
}
}
`;
// get a mutation document
const ADD_USER = gql`
mutation myMutation($uid: String, $lastname: String) {
addUser(uid: $uid, lastname: $lastname) {
uid
lastname
}
}
`;
export { graphql, createKaptonMixin, USERS_LIST, ADD_USER };
In this example Webpack configuration will use a library "var" output with the library set to "App", so exported variables will be accessible through the global "App" object (for example App.graphql or App['graphql']).
This small example contains a query and a mutation.
<link rel="import" href="a-piece-of-html-loading-webpacked-js.html">
<dom-module id="my-graphql">
<template>
<style>
:host {
display: block;
padding: 10px;
}
</style>
<ul>
<template is="dom-repeat" items="{{data.users}}">
<li>{{item.uid}} : {{item.lastname}}</li>
</template>
</ul>
<div>
<input id="uidInput" type="text">
<input id="lastnameInput" type="text">
<button on-tap="_createUser">Create</button>
</div>
</template>
<script>
const UserListMixin = App.createKaptonMixin(App.graphql, App.USERS_LIST, 'myQueryOpts');
const AddUserMixin = App.createKaptonMixin(App.graphql, App.ADD_USER, { name: "addUser" });
class MyGraphql extends UserListMixin(AddUserMixin(Polymer.Element)) {
static get is() { return 'my-graphql'; }
static get properties() {
return {
authenticated: Boolean,
limit: Number,
// each time limit or authenticated properties will change, your query
// will be updated and sent to server if needed.
myQueryOpts: {
type: Object,
computed: '_computeMyQueryOptions(limit, authenticated)',
},
// the default variable receiving query result
data: Object,
};
}
_computeMyQueryOptions(limit, authenticated) {
return {
variables: {
limit: limit,
},
forceFetch: true,
skip: !authenticated,
};
}
_createUser() {
// mutate is the default name of the mutation function added to the
// element
this.addUser({
variables: {
uid: this.$.uidInput.value,
lastname: this.$.lastnameInput.value,
}
}).then(function(result) {
this._resetForm();
}.bind(this));
}
}
customElements.define(MyGraphql.is, MyGraphql);
</script>
In all examples, we'll assume the mixin factory is App.graphql (seeusage example above).
If your element have more than one Kapton mixin, it can be useful to use the helper function createKaptonMixin :
mixin = createKaptonMixin(factory, document, options)
Mixin factory can be used this ways :
// using reactive options
App.graphql(queryOrSubscriptionDocument, "options_property_name", superClass);
// using static options
App.graphql(queryOrSubscriptionDocument, { name: "myData", variables: { foo: "bar " } }, superClass);
// using default options
App.graphql(queryOrSubscriptionDocument, null, superClass);
Subscriptions will require more configuration and set-up you've seen in the example above. Check Apollo graphql-subscriptions and subscriptions-transport-ws project on github. It implements GraphQL subscriptions using websocket.
The option object can contains the following keys:
The result contains all the keys you'd have found in the data key of the graphql result. It also contains the following keys allowing advanced usages :
The result only contains the key(s) you'd have found in the data key of the graphql result.
// without options
App.graphql(mutationDocument, null, superClass);
// using static options
App.graphql(mutationDocument, { name: "createFoo" }, superClass);
The option object can contains the following keys:
The mutation function (aka "mutate") can also contains options (variables, optimisticResponse, updateQueries, ...). See apollo-client documentation.
the addUser mutation can use more advanced features of Apollo client, for example optimisticResponse and updateQueries. Check Apollo documentation for more informations. In the first example, the addUser result will not be added to the user list. The following code fix this problem :
_createUser() {
// mutate is the default name of the mutation function added to the
// element
this.addUser({
variables: {
uid: this.$.uidInput.value,
lastname: this.$.lastnameInput.value,
},
// generate a fake result to speed-up UI. To incorporate it to the
// query above, you'll have to use updateQueries.
// This is optional.
optimisticResponse: {
addUser: {
__typename: 'User',
uid: this.$.uidInput.value,
lastname: this.$.lastnameInput.value,
}
},
// this updateQueries will add the mutation result (the real one as
// well as the optimistic one) to the "users" list in the store. It
// means dependings queries will be updated.
// This is optional.
updateQueries: {
myQuery: (prev, { mutationResult }) => {
return Object.assign({}, prev, { users: [ ...prev.users, mutationResult.data.addUser ] });
}
}
}).then(function(result) {
this._resetForm();
}.bind(this));
}
FAQs
Polymer and Apollo client Integration
We found that kapton demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.